#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int mxN = 75*1e3;
int t;

ll Nzd(ll a, ll b){

    while (b){
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

void Solve(){

    int n, m;
    ll nzd, a[mxN], b[mxN];
    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);
    nzd = a[1] - a[0];
    for (int i = 2; i < n; i++)
        nzd = Nzd(nzd, a[i] - a[i-1]);
    for (int i = 0; i < m; i++){
        if ((b[i] - a[0]) % nzd == 0){
            cout << "0" << endl;
            return;
        }
    }
    cout << "1" << endl;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> t;
    while (t--)
        Solve();

    return 0;
}