#include <bits/stdc++.h>
#define K int(1e9 + 10)
#define INF int(1e9)
#define MOD (long long)(1e9 + 7)
#define endl "\n"
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;

void solve(){
    int n, m;
    cin >> n >> m;
    ull tmp;
    vector<pair<ull, bool>> v;
    while (n--){
        cin >> tmp;
        v.push_back({tmp, 1});
    }
    while (m--){
        cin >> tmp;
        v.push_back({tmp, 0});
    }
    sort(v.begin(), v.end());
    int lst1 = -1;
    vector<ull> xs;
    for (int i = 0; i < v.size() && lst1 < 0; i++)
        if (v[i].second == 1) lst1 = i;
    for (int i = lst1 + 1; i < v.size(); i++)
        if (v[i].second == 1){
            xs.push_back(v[i].first - v[lst1].first);
            lst1 = i;
        }
    ull x = xs[0];
    for (ull i : xs) x = __gcd(x, i);
    for (int i = 0; i < v.size() - 1; i++){
        if ((v[i + 1].first - v[i].first) % x == 0 && v[i + 1].second * v[i].second == 0){
            cout << 0 << endl;
            return;
        }
    }
    cout << 1 << endl;
}
 
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    int t = 1;
    cin >> t;
    while (t--)
        solve();
 
    return 0;
}