#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define endl "\n"

using namespace std;

ll gcd(ll x, ll y) {
    return !y?x:gcd(y,x%y);
}

void solve()
{
    int n,m; cin >> n >> m;

    vector<ll> a(n);
    for(auto& x:a) cin >> x;

    vector<ll> b(m);
    for(auto& x:b) cin >> x;

    sort(a.begin(), a.end());

    vector<ll> dif(n-1);
    for(int i=0;i<n-1;i++)
        dif[i]=a[i+1]-a[i];

    ll x=dif[0];
    for(ll y:dif)
        x=gcd(x,y);

    ll levo=a[0]%x;
    for(ll d:b) {
        if(d%x==levo) {
            cout << "0" << endl;
            return;
        }
    }

    cout << "1" endl;

}

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

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

    return 0;
}