#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 75005;

ll n,m,a[N],b[N];
ll gcdpf[N],gcdsf[N];
ll li[N];

void solve(){
    cin >> n >> m;

    for(int i = 1; i <= max(n,m); i++){
        li[i] = gcdpf[i] = gcdsf[i] = 0;
    }

    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= m; i++){
        cin >> b[i];
    }

    sort(a+1,a+n+1);

    ll g = 0;
    for(int i = 2; i <= n; i++){
        g = __gcd(g,a[i]-a[i-1]);
        gcdpf[i] = __gcd(gcdpf[i-1],a[i]-a[i-1]);
    }
    for(int i = n-1; i >= 1; i--){
        gcdsf[i] = __gcd(gcdsf[i+1],a[i+1]-a[i]);
    }

    for(int i = 1; i <= m; i++){
        int l = 1,r = n,ans = 0;
        while(l <= r){
            int mid = l+(r-l)/2;
            if(a[mid] < b[i]){
                l = mid+1;
                ans = mid;
            }
            else{
                r = mid-1;
            }
        }

        if(ans == 0){ /// skroz levo
            li[i] = __gcd(a[1]-b[i],gcdsf[1]);
        }
        else if(ans == n){
            li[i] = __gcd(gcdpf[n],b[i]-a[n]);
        }
        else{
            li[i] = __gcd(gcdpf[ans],gcdsf[ans+1]);
            li[i] = __gcd(li[i],b[i]-a[ans]);
            li[i] = __gcd(li[i],a[ans+1]-b[i]);
        }
    }

    for(int i = 1; i <= m; i++){
        ll x = __gcd(li[i],g);
        g /= x;
    }

    cout << (g > 1) << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}