#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,ll) sort(a.begin(),a.end(),greater<ll>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define up(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define vstart auto startt=chrono::system_clock::now()
#define vend auto endd=chrono::system_clock::now()
#define vvreme chrono::duration<double> vremee=endd-startt
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<ll,ll> pll;
typedef string str;

const ll maxn=75001;
ll niz1[maxn];
ll niz2[maxn];

void solve(){
    ll n,m;
    cin>>n>>m;
    for(ll i=0;i<n;i++){
        cin>>niz1[i];
    }
    sort(niz1,niz1+n);
    ll gcd=0;
    ll prva=niz1[0];
    for(ll i=1;i<n;i++){
        gcd=__gcd(gcd,niz1[i]-niz1[i-1]);
    }
    bool ok=true;
    for(ll i=0;i<m;i++){
        ll a;
        cin>>a;
        if(abs(a-prva)%gcd==0) ok=false;
    }
    cout<<(ok ? 1:0)<<"\n";
}

int main(){
    ios;
    ll t=1;
    cin>>t;
    while(t--) solve();
    return 0;
}