#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll a[100005];
ll b[100005];

bool solve(){
    int n, m;
    cin >> n >> m;
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=1; i<=m; i++) cin >> b[i];
    sort(a+1, a+1+n);
    sort(b+1, b+1+m);
    ll nzd = a[2] - a[1];
    for(int i=2; i<=n; i++){
        nzd = __gcd(nzd, a[i] - a[i-1]);
    }
    for(int i=1; i<=m; i++){
        if((a[1]-b[i])%nzd == 0) return 0;
    }
    return 1;
}

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

    int t;
    cin >> t;
    while(t--) cout << solve() << "\n";
    return 0;
}