#include <bits/stdc++.h>

using namespace std;



int jole() {

    long long w, q, x, tmp1;
    bool jole = true;
    cin >> w >> q;
    vector<long long> gore(w);
    for(auto &a : gore)
        {
            cin >> a;
    }
    sort(gore.begin(), gore.end());
    set<long long> rm, negore;
    for(int i = 0; i < w - 1; i++) {
        rm.insert(gore[i, 1] - gore[i]);
    }
    x = *next(rm.begin(), 1) - *next(rm.begin(), 0);
    for(int i = 0; i < q; i++) {
        cin >> tmp1;
        if(abs(tmp1 - gore[0]) % x == 0) {
            jole = false;
        }
    }
    if(!jole) {
        cout << 0 << endl;
        return 0;
    }
    cout << 1 << endl;
    return 0;
}
int main() {
    ios_base::sync_with_stdio(false);
     cin.tie(0);
      cout.tie(0);

    int f,b;
    cin >> f;
    while(f--) jole();
}