function euclide(a,b : int64) : int64;
        var res : int64;
         begin
                if (b = 0) then res := a else
                        if (a = 0) then res := b else
                if (a > b) then res := euclide(a mod b, b) else
                res := euclide(b mod a, a);
                euclide := res;

         end;
var t : text;
i,k,j: longint;
        n,m,nok : int64;
       res2, res : int64;
        a : array[1..1001] of longint;
begin
        assign(t,'numbers.in');
        reset(t);
        readln(t,K);
        for i := 1 to k do begin
                readln(t,n,m);
                for j := 1 to n do begin
                read(t,a[j]);
                a[j] := a[j] mod m;
                end;

                res := a[1];
                nok := a[1];
                for j := 2 to n do begin
                res := euclide(res,a[j]);

                res2 := euclide(nok,a[j]);

                nok := nok*a[j] div res2 mod m;
                end;
                writeln(res,' ',nok);
        end;
        close(t);
end.
