//412_4
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>

#define min(a, b) (((a)<(b))?(a):(b))
#define max(a, b) (((a)>(b))?(a):(b))

using namespace std;

const int N = 1e5;
const int M = 1e6;
const int oo = 1e9;

typedef long long ll;

int p[M], a[N], nd[N], nk[N];

void answer()
{
    int i, j, n, l, c;
    long long  m, annd, annk;

    scanf("%d %lld", &n, &m);

    l = p[0];
    for(i=0; i<l; ++i)
    {
        nd[i] = oo;
        nk[i] = 0;
    }

/*
    for(j=1; j<10; ++j)
        printf("%d\n", p[j]);
  */
    for(i=0; i<n; ++i)
    {
        scanf("%d", &c);
        for(j=1; j<l; ++j)
            a[j] = 0;


        for(j=1; j<l; ++j)
        {
            while((c%p[j])==0)
            {
                ++a[j];
                c /= p[j];
               // printf("%d %d %d\n", c, p[j], j);
            }

        }


        if(c>1)
        {
            p[l] =  c;
            a[l] = 1;
            nd[l] = oo;
            nk[l] = 0;
            ++l;
        }

        for(j=1; j<l; ++j)
        {
            nd[j] = min(nd[j], a[j]);
            nk[j] = max(nk[j], a[j]);
        }
        // printf("\n\n");

    }
    //printf("OK\n");
    annd = 1;
    annk = 1;
    for(j=1; j<l; ++j)
    {
      //  if(j<10)
           // printf("%d %d %d %d\n", nd[j], nk[j], j, p[j]);
        if(nd[j] != oo )
        {
            while(nd[j]--)
                annd = annd*p[j];
        }
        if(nk[j]!=oo)
        {
            while(nk[j]--)
                annk = (annk*p[j])%m;
        }
    }

    printf("%lld %lld\n", annd, annk%m);

}
int main()
{
    freopen("NUMBERS.IN","r", stdin);
    //freopen("input.txt", "r", stdin);
    int t,n,m,x;
    vector <pair <int, int > > v;
    scanf("%d", &t);

    while(t--)
    {
        scanf("%d%d", &n,&m);
        v.clear();

        for(int i=0;i<n;++i)
        {
            scanf("%d",&x);
            for(int j=2;j*j<=x;++j)
            {
                int c=0;
                while (x%j==0)
                {
                    x/=j;
                    c++;
                }
                if (c)
                    v.push_back(make_pair(j,c));
            }
            if (x>1)
                v.push_back(make_pair(x,1));
        }

        sort(v.begin(),v.end());

        ll nod=1,nok=1;
     //   for(int i=0;i<v.size();++i) printf("%d %d\n",v[i].first,v[i].second);
        int l=0,r=0;
        while (l<v.size())
        {
            if (r<v.size() && v[r].first==v[l].first) r++;
            else
            {
                if (r-l==n)
                    for(int i=0;i<v[l].second;++i)
                        nod*=v[l].first;
                for(int i=0;i<v[r-1].second;++i)
                    nok=(nok*v[r-1].first)%m;

                l=r;
            }
        }
        printf("%I64d %I64d\n",nod,nok%m);
    }

    return 0;
}

