(Optional Question) Moriarty's Deceit

3.6

248 votes
Mathematics, Easy
Problem

The evil mastermind, Moriarty, strikes again! He sent Sherlock an image of a wall, with N integers painted on it. Sherlock is asked to solve the puzzle and find the next clue.
Sherlock knows that Moriarty is a deceitful person. All the numbers painted on the wall are not valid. Only the numbers that are Coprime with the integer X are valid, and the rest are a deceit. Help Sherlock find the valid integers from the given array A so that he can track down Moriarty.

Input:
First line contains T. T test cases follow.
First line of each test case contains two space-separated integers N and X.
Second line of each test case contains N space-separated integers, forming the array A.

Output:
Print the valid integers separated by spaces in the order that they appear in the list or "-1" (without the quotes) if there are no valid integers, in a new line for each test case.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 104
2 ≤ Ai, X ≤ 107

Time Limit: 3
Memory Limit: 256
Source Limit:
Editor Image

?