#include <iostream>
#include <algorithm>

using namespace std;

long long *read(long long n) {
    long long *t = new long long[n];
    for (int i = 0; i < n; i++)
        scanf("%lld", t + i);
    return t;
}

long long gcd(long long a, long long b) {
    if (a < b)
        swap(a, b);
    while (b != 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}

int main() {
    int t;
    cin >> t;
    for (; t > 0; t--) {
        int n, m;
        scanf("%d%d", &n, &m);
        long long *a = read(n);
        long long *b = read(m);

        if (n == 1) {
            cout << 1;
        } else {
            long long d = a[1] - a[0];
            for (int i = 2; i < n; i++) {
                d = gcd(d, a[i] - a[i - 1]);
            }
            long long p = a[0] % d;
            bool k = true;
            for (int i = 0; i < m && k; i++) {
                if (p == b[i] % d)
                    k = false;
            }
            cout << k;
        }
        cout << endl;

        delete[] a;
        delete[] b;
    }
    return 0;
}