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

#define ll long long

ll t;
ll n, m;
ll a[75005];

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

    cin >> t;

    while(t--){
        cin >> n >> m;

        ll mini;
        for(ll i = 0; i < n; ++i){
            cin >> a[i];
            if(i == 0){mini = a[0];}
            else{mini = min(mini, a[i]);}
        }

        ll gcd = -1;
        for(ll i = 0; i < n; ++i){
            if(a[i] == mini){continue;}
            if(gcd == -1){gcd = a[i] - mini;}
            else{gcd = __gcd(gcd, a[i] - mini);}
        }

        bool res = true;
        ll r = mini % gcd;
        for(ll i = 0; i < m; ++i){
            ll b;
            cin >> b;
            if(b % gcd == r){res = false; continue;}
        }

        cout << res << "\n";
    }

    return 0;
}