#include <bits/stdc++.h>
using namespace std;

long long a[75005];
long long b[75005];

void solve(){
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i = 0; i < m; ++i){
        cin >> b[i];
    }
    sort(a, a + n);
    sort(b, b + m);
    long long gcd = a[1] - a[0];
    for(int i = 2; i < n; ++i){
        gcd = __gcd(a[i] - a[i - 1], gcd);
    }
    bool ok = true;
    long long off = a[0] % gcd;
    for(int i = 0; i < m; ++i){
        if(b[i] >= off && (b[i] - off) % gcd == 0){
            ok = false;
            break;
        }
    }

    cout << (ok ? 1 : 0) << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}