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

/**
 * Created with IntelliJ IDEA.
 * User: student
 * Date: 10.11.13
 * Time: 13:44
 * To change this template use File | Settings | File Templates.
 */
public class K {

    static int M;

    public static long NOD(long x, long y) {
        long a = Math.max(x, y);
        long b = Math.min(x, y);

        if (a == b || b == 0) return a;
        else return NOD(b, a%b);
    }

    static Map<Long, Long> mapFirstDividers = new HashMap<Long, Long>();
    static Map<Long, Map<Long, Long>> mapDividers = new HashMap<Long, Map<Long, Long>>();

    static long getFirstDivider (long x)
    {
        if (mapFirstDividers.containsKey(x)) return mapFirstDividers.get(x);

        long res = x;
        for (int i = 2; i < Math.sqrt(x); i++) {
            if (x % i == 0){
                res = i;
                break;
            }
        }

        mapFirstDividers.put(x, res);
        return res;
    }

    static Map<Long, Long> getDividers(long x)
    {
        if (mapDividers.containsKey(x)) return mapDividers.get(x);

        Map<Long, Long> res = new HashMap<Long, Long>();

        while (x != 1)
        {
            long div = getFirstDivider(x);
            if (res.containsKey(div))
                res.put(div, res.get(div)+1L);
            else
                res.put(div, 1L);
            x /= div;
        }

        mapDividers.put(x, res);
        return res;
    }

    static public long commonNod(long[] xs) {
        long x = xs[0];

        for (int i = 1; i < xs.length; i++) {
            x = NOD(x, xs[i]);
        }

        return x;
    }

    static public long commonNok(long[] xs) {
        Map<Long, Long> res = new HashMap<Long, Long>();
        for (int i = 0; i < xs.length; i++) {
            Map<Long, Long> dividers = getDividers(xs[i]);
            for (Long d : dividers.keySet()) {
                if (res.containsKey(d)) {
                    res.put(d, Math.max(res.get(d), dividers.get(d)));
                } else {
                    res.put(d, dividers.get(d));
                }
            }
        }

        long longRes = 1;

        for (Map.Entry<Long, Long> entry : res.entrySet()) {
            Long k = entry.getKey();
            Long v = entry.getValue();

            for (int i = 0; i < v; i++) {
                longRes *= k;
                longRes %= M;
            }
        }

        return longRes;

    }

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("NUMBERS.IN"));

        int T = scanner.nextInt();

        for (int i = 0; i < T; i++) {
            int N = scanner.nextInt();
            M = scanner.nextInt();

            long[] nums = new long[N];

            for (int j = 0; j < N; j++) {
                nums[j] =  scanner.nextLong();
            }

            System.out.println(commonNod(nums) + " " + commonNok(nums));
        }
    }
}
