#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
    return (b == 0 ? a : gcd(b, a % b));
}

void read(vector<ll> &a) {
    for (ll &i : a) {
        cin >> i;
    }
}

void solve() {
    int n, m; cin >> n >> m;
    vector<ll> a(n), b(m);
    read(a); read(b);
    sort(a.begin(), a.end());
    ll x = a[1] - a[0];
    for (int i = 2; i < n; i++) {
        x = gcd(x, a[i] - a[i - 1]);
    }
    bool f = true;
    for (int i = 0; i < m; i++) {
        f &= (a[0] % x != b[i] % x);
    }
    cout << f << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}