import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Numbers 
{
	public static void main(String[] args) throws Exception
	{
		new Numbers().run();
	}
	
	private int gcdf(int a, int b) 
	{
		while (b > 0) 
		{
			a %= b;
			int buf  = a;
			a = b;
			b = buf;
		}
		return a;
	}
	
	private void run() throws Exception 
	{
		Scanner in = new Scanner(new File("numbers.in"));
		PrintWriter out = new PrintWriter(System.out);
		
		int MAX_N = 40000;
		int MAX_SIZE = 1000;
		
		int pn = 0;
		int[] p = new int[MAX_N];
		p[pn++] = 2;
		
		boolean[] composite = new boolean[MAX_N];
		
		for (int i = 3; i < MAX_N; i+= 2)
		{
			if (!composite[i])
			{
				p[pn++] = i;
				if ((long)i * (long)i < MAX_N)
				{
					for (int j = i * i; j < MAX_N; j += i)
					{
						composite[j] = true;
					}
				}
			}
		}
		
		int testn = in.nextInt();
		for (int testi = 0; testi < testn; testi++)
		{
			int n = in.nextInt();
			if (n <= 0 || n > MAX_SIZE)
				throw new Exception("N out of bounds. Test:" + testi);
			
			int mod = in.nextInt();
			if (mod <= 0 || mod > 1000000000)
				throw new Exception("MOD out of bounds. Test:" + testi);
		
			int[] a = new int [MAX_SIZE];
			for (int i = 0; i < n; i++)
			{
				a[i] = in.nextInt();
				if (a[i] <= 0 || a[i] > 1000000000)
					throw new Exception("A[i] out of bounds. Test:" + testi);
			}
			
			int gcd = a[0];
			for (int i = 0; i < n; i++)
			{
				gcd = gcdf(gcd, a[i]);
			}
		
			HashMap<Integer, Integer> fact = new HashMap<Integer, Integer>();
			for (int i = 0; i < n; i++)
			{
				for (int pi = 0; pi < pn; pi++)
				{
					if (a[i] == 1)
						break;
					
					int count = 0;
					while (a[i] % p[pi] == 0)
					{
						count++;
						a[i] /= p[pi];
					}
					if (count > 0)
					{
						Integer key = Integer.valueOf(p[pi]);
						Integer value = Integer.valueOf(count);
						if (!fact.containsKey(key))
						{
							fact.put(key, value);
						}
						else if (fact.get(key).intValue() < value.intValue())
						{
							fact.remove(key);
							fact.put(key, value);
						}
					}
				}
			
				if (a[i] > 0)
				{
					Integer key = Integer.valueOf(a[i]);
					Integer value = Integer.valueOf(1);
					if (!fact.containsKey(key))
					{
						fact.put(key, value);
					}
					else if (fact.get(key).intValue() < value.intValue())
					{
						fact.remove(key);
						fact.put(key, value);
					}
				}
			}
		
			long lcm = 1;
		
			for(Map.Entry<Integer, Integer> entry : fact.entrySet()) 
			{
				Integer key = entry.getKey();
				Integer value = entry.getValue();
				
				for (int i = 0; i < value; i++)
				{
					lcm = lcm * key % mod;
				}
			}
			
			out.println(gcd + " " + lcm);
		}
		
		in.close();
		out.close();
	}
}
