import java.util.Scanner;

public class Treci {

    public static void main(String[] args) {
        Scanner ul = new Scanner(System.in);
        int T = ul.nextInt();
        int M, N;

        boolean ne = true;
        int[] resenje = new int[T];
        for (int i = 0; i < T; i++) {
            ne = true;
            N = ul.nextInt();
            M = ul.nextInt();
            long[] prihvacene = new long[N];
            for (int j = 0; j < N; j++) {
                prihvacene[j] = ul.nextLong();
            }
            long[] odbijene = new long[M];
            for (int j = 0; j < M; j++) {
                odbijene[j] = ul.nextLong();
            }

            long minPri = Math.abs(prihvacene[0] - prihvacene[1]);
            for (int j = 1; j < N - 1; j++) {
                if (Math.abs(prihvacene[j] - prihvacene[j + 1]) < minPri) {
                    minPri = Math.abs(prihvacene[j] - prihvacene[j + 1]);
                }
            }

            for (int j = 1; j < N - 1; j++) {
                if (Math.abs(prihvacene[j] - prihvacene[j + 1]) % minPri != 0) {
                    ne = false;
                }
            }
            for (int j = 0; j < M - 1; j++) {
                if (Math.abs(odbijene[j] - odbijene[j + 1]) % minPri == 0) {
                    ne = false;
                }
            }

            if (ne == true) {
                resenje[i] = 1;
            }
        }
        for (int i = 0; i < T; i++) {
            System.out.println(resenje[i]);
        }

    }
}