#include <iostream>
#include <cmath>

using namespace std;
long long nzd(long long a, long long b) {
    if (a == 0)
        return b;
    return nzd(b % a, a);
}

int main() {
    ios_base::sync_with_stdio();
    long long N;
    cin >> N;
    long long n, m, a, b, c, d;
    for (long long i = 0; i < N; i++) {
        cin >> n >> m;
        cin >> a >> b;
        c = abs(a - b);
        for (long long j = 0; j < n-2; j++) {
            cin >> d;
            c = nzd(c, a - d);
        }
        bool tren = true;
        for (long long z = 0; z < m; z++) {
            cin >> d;
            if (tren && abs(a-d) % c == 0) {
                tren = false;
            }
        }
        if (tren)
            cout << 1 << "\n";
        else
            cout << 0 << "\n";
    }
    return 0;
}