#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll nzd(ll a[], ll n)
{
    ll j = a[0];
    for (int i = 1; i < n; i++)
    {
        j = __gcd(a[i], j);
        if(j == 1)
        {
           return 1;
        }
    }
    return j;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
    int t,n,m;
    cin >> t;
    int ans[t];
    for(int j=0;j<t;++j){
        cin >> n >> m;
        ll a[n];
        ll b[m];
        ll razlika[n-1];
        ans[j] = 1;
        for(int i=0;i<n;++i){
            cin >> a[i];
        }
        for(int i=0;i<m;++i){
            cin >> b[i];
        }
        sort(a,a+n);
        int z = 0;
        while(z<n-1){
            razlika[z] = a[z+1] -a[z];
            ++z;
        }
        ll pom = nzd(razlika, n-1);
        ll x = a[0];
        z = 0;
        while(x>b[0]){
            while(z<m){
                if(x==b[z]){
                    ans[j] = 0;
                }
                ++z;
            }
            z=0;
            x-=pom;
        }
        x = a[0];
        z = 0;
        while(x<b[m-1]){
            while(z<m){
                if(x == b[z]){
                    ans[j] = 0;
                }
                ++z;
            }
        z=0;
        x+=pom;
    }
}
    for(int i=0;i<t;++i){
    cout << ans[i] << "\n";
    }
    return 0;
}