#include <iostream>
#include <stdio.h>

#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;

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])%m;
        }
        if(nk[j]!=oo)
        {
            while(nk[j]--)
                annk = (annk*p[j])%m;
        }
    }

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

}
int main()
{
    freopen("NUMBERS.IN","r", stdin);
    //freopen("input.txt", "r", stdin);

    int i, j, t, ok;
    long long nk, nd, ym;

    p[0] = p[1] = 2;

    for(i=3; i<N; ++i)
    {
        ok = true;
        j = 1;
        while(p[j]*p[j]<=i)
        {
            if(!(i%p[j]))
            {
                ok = false;
                break;
            }
            ++j;
        }

        if(ok)
            p[p[0]++] = i;
    }

    scanf("%d", &t);

    while(t--)
    {
        answer();
    }
    return 0;
}

