#include <iostream>

using namespace std;

long a[75000], b[75000];
long provera (int n, int m){
    int c, d = 0;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int i = 0; i < m; i++)
        cin >> b[i];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n - 1; j++)
            if (a[j] > a[j + 1])
                swap(a[j], a[j + 1]);
    c = a[1] - a[0];
    for (int i = 2; i < n; i++)
        if (a[i] - a[i - 1] < c) c = a[i] - a[i - 1];
    for (int i = 1; i < n; i++)
        if ((a[i] - a[i - 1]) % c == 0) d++;
    if (d == n - 1) return 1;
    else return 0;
}

int main()
{
    int t, n, m;
    cin >> t;
    int r[t];
    for (int i = 0; i < t; i++){
        cin >> n >> m;
        r[i] = provera(n, m);
    }
    for (int i = 0; i < t; i++)
        cout << r[i] << endl;
	return 0;
}