#include <iostream>
#include <algorithm>
using namespace std;
long long nzd(long long a, long long b)
{
    if(a<b) return nzd(b,a);
    if(a%b==0) return b;
    return nzd(b,a%b);
}
int main()
{
    int t;
    long n,m;
    cin>>t;
    int rjes[t];
    for(int T=0;T<t;T++){
    cin>>n>>m;
    long long a[n],b[m];
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<m;i++) cin>>b[i];
    sort(a,a+n);
    long long nzdA=abs(a[0]-a[1]);
    for(int i=2;i<n;i++) nzdA=nzd(nzdA,abs(a[i]-a[i-1]));
    //cout<<"nzdA:"<<nzdA<<endl;
    rjes[T]=1;
    for(int i=0;i<m && rjes[T]==1;i++)
    {
        if(abs(b[i]-a[0])%nzdA==0) rjes[T]=0;
    }
    //cout<<"nzdA:"<<nzdA<<endl;
    }
    for(int T=0;T<t;T++) cout<<rjes[T]<<endl;
    return 0;
}