#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n,m;
    cin >> n >> m;

    int a[n+5], b[m+5];
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for(int i = 1; i <= m; i++) {
        cin >> b[i];
    }

    bool postoji;
    for(int br = 2; br <= 1000000; br++) {
        int ostatak = a[1] % br;
        postoji = true;
        for(int i=1; i<=n; i++) {
                //cout << a[i] % br << " -> ";
                if(a[i]%br != ostatak)
                {
                    postoji = false;
                    break;
                }
        }
        int j = 1;
        while(postoji && j <= m) {
                //cout << b[i] % br << " <- ";
                if(b[j]%br == ostatak)
                {
                    postoji = false;
                }

                j++;
        }
        if(postoji)
        {
            cout << 1 << endl;
            return;
        }
    }

    cout << 0 << endl;
    return;
}
int main()
{
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}