#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second

using namespace std;

ll n, m, a[75010], b[75010];

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

    //cout<<__gcd(1, 1010010)<<endl;

    int t; cin>>t;
    while (t--)
    {
        cin>>n>>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);
        sort(b, b+m);

        ll gcdd=abs(a[1]-a[0]);
        for (int i=1; i<n; i++) gcdd=__gcd(gcdd, abs(a[i]-a[i-1]));

        ll ress=gcdd;
        for (int i=0; i<m; i++)
        {
            ll tren=abs(b[i]-a[0]);
            ress=(ress/__gcd(ress, tren));
        }
        if (ress==1) { cout<<0<<endl; }
        else cout<<1<<endl;

        /*ll lcmm=abs(b[0]-a[0]);
        for (int i=0; i<m; i++) lcmm=nzs(lcmm, abs(b[i]-a[0]));

        if (gcdd/__gcd(gcdd, lcmm)!=1) { cout<<gcdd/__gcd(gcdd, lcmm)<<endl; }
        else cout<<0<<endl;*/
    }

}