#include <bits/stdc++.h>

using namespace std;

#define zuri8 ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int resi() {
    long long n, m, x, tmp;
    bool res = true;
    cin >> n >> m;
    vector<long long> more(n);
    for(auto &a : more) cin >> a;
    sort(more.begin(), more.end());
    set<long long> razlikeM, nemore;
    for(int i = 0; i < n - 1; i++) {
        razlikeM.insert(more[i, 1] - more[i]);
    }
    x = *next(razlikeM.begin(), 1) - *next(razlikeM.begin(), 0);
    for(int i = 0; i < m; i++) {
        cin >> tmp;
        if(abs(tmp - more[0]) % x == 0) {
            res = false;
        }
    }
    if(!res) {
        cout << 0 << endl;
        return 0;
    }
    cout << 1 << endl;
    return 0;
}
int main() {
    zuri8;
    int t;
    cin >> t;
    while(t--) resi();
}