#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

ll nzd(ll a, ll b){
    if(a < b){
        int c = a;
        a = b;
        b = c;
    }
    if(b <= 1){
        return a;
    }
    else{
        return nzd(b, a%b);
    }
}

int main()
{
    ll t, n, m, b, a, ost;
    vector<ll> vec;
    cin >> t;
    vector<int> res(t);
    for(int i = 0; i < t; ++i){
        cin >> n >> m;
        vec.resize(n);
        for(int j = 0; j < n; ++j){
            cin >> vec[j];
        }
        sort(vec.begin(), vec.end());
        a = vec[1] - vec[0];
        for(int j = 2; j < n; ++j){
            a = nzd(vec[j]-vec[j-1], a);
        }
        ost = vec[0]%a;
        res[i] = 1;
        for(int j = 0; j < m; ++j){
            cin >> b;
            if(b%a == ost){
                res[i] = 0;
            }
        }
    }
    for(int i = 0; i < t; ++i){
        cout << res[i] << endl;
    }
    return 0;
}