#include <bits/stdc++.h>

using namespace std;

long long nzd(long long a, long long b){
    if (b == 0){
        return a;
    }
    return nzd(b, a % b);
}

int main()
{
    ios_base::sync_with_stdio(0);
    short t;
    cin >> t;
    for(short i = 0; i < t; i++){
        int n, m;
        cin >> n >> m;
        long long z, a, b, minn;
        cin >> a >> b;
        minn = min(a, b);
        z = abs(a - b);
        for(int j = 0; j < n - 2; j++){
            a = b;
            cin >> b;
            if(b < minn){
                minn = b;
            }
            z = nzd(z, abs(a - b));
        }
        bool da = false;
        for(int j = 0; j < m; j++){
            cin >> a;
            if((a - minn) % z == 0){
                da = true;
            }
        }
        if(da == true){
            cout << 0 << endl;
        }
        else{
            cout << 1 << endl;
        }
    }
    return 0;
}