#include <iostream>
#include <fstream>
#include <map>
using namespace std;

long long gcd(long long a, long long b) {
	while(a > 0) {
		a^=b^=a^=b%=a;
	}
	return b;
}
//long long a[1000003], b[1000003];
int mark[100000];
int main() {
	ifstream I("NUMBERS.IN");
	int t;
	long long n, m;
	for (I >> t; t--; ){
		long long a, b, x, y;
		map<long long, int> map1;
		I >> n >> m;
		I >> y;
		a = y;
		b = 1;
		//cout << a << " " << b << " is ";
		for (; n-- > 1; ){
			x = y;
			int z = x;
			for (int i = 2; i*i <= x; i++){
				int count = 0;
				for (; z % i < 1;){
					z /= i;
					count++;
				}
				if (mark[i] < count)
					mark[i] = count;
			}
			if (z >= 100000){
				if (!map1[z])
					map1[z] = 1,
					b = b * z % m;
			} else if (!mark[z]){
				mark[z] = 1;
			}
			I >> y;
			a = gcd(a, y);
		}

		int z = y;
		for (int i = 2; i*i <= y; i++){
			int count = 0;
			for (; z % i < 1;){
				z /= i;
				count++;
			}
			if (mark[i] < count)
				mark[i] = count;
		}

		if (z >= 100000){
			if (!map1[z])
				map1[z] = 1,
				b = b * z % m;
		} else if (!mark[z]){
			mark[z] = 1;
		}
		

		for (int i = 2; i < 100000; i++){
			for (; mark[i] > 0; mark[i]--){
				//cout << i << " " << mark[i] << " == ";
				b = b*i % m;
			}
		}
		cout << a << " " << b % m << "\n";
		//long long s = 454 * 1374 * 466 * 1434;
		//cout << b % 454 << "\n";
	}
	return 0;
}