#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
int main()
{
    ll t,n,m;
    cin>>t;
    ll c[t],x=0;
    for(ll i=0;i<t;i++)
    {
        cin>>n>>m;
        ll a[n],b[m];
        for(ll u=0;u<n;u++){
            cin>>a[u];
        }
        for(ll u=0;u<m;u++){
            cin>>b[u];
        }
        sort(a,a+n);
        ll mini=a[1]-a[0];
        ll ima=0;
        while(mini>1)
        {
            for(ll u=0;u<m;u++)
            {
                if((b[u]-a[0])%mini!=0)
                {
                    ima=1;
                    break;
                }
            }
            if(ima==1)
            {
                break;
            }
            mini--;
        }
        if(ima==1){c[x]=1;x++;}else{c[x]=0;x++;}
    }
    for(ll i=0;i<t;i++)
    {
        cout<<c[i]<<endl;
    }
}