#include <bits/stdc++.h>

#define ll long long

using namespace std;

ll gcd(ll a, ll b) {

    if(b == 0)
        return a;
    return gcd(b, a % b);
}

void solve() {

    int n, m;
    cin >> n >> m;

    ll a[n];
    for(int i = 0; i < n; i++)
        cin >> a[i];
    sort(a, a + n);
    ll b[m];
    for(int i = 0; i < m; i++)
        cin >> b[i];

    if(n == 1) {
        cout << 1 << endl;
        return;
    }

    ll x = 0;
    for(int i = 0; i < n - 1; i++)
        x = gcd(a[i + 1] - a[i], x);

    for(int i = 0; i < m; i++) {
        if((b[i] - a[0]) % x == 0) {
            cout << 0 << endl;
            return;
        }
    }
    cout << 1 << endl;
}

int main() {

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

    int t;
    cin >> t;
    while(t--)
        solve();
    return 0;
}