#include <bits/stdc++.h>

using namespace std;

#define int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

signed main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr); 
    cerr.tie(nullptr);    

    int t;
    cin >> t;

    for (int e = 0; e < t; ++e){
        int n, m;
        cin >> n >> m;

        vi a(n);
        for (auto& i : a)
            cin >> i;
        sort(a.begin(), a.end());

        vi b(m);
        for (auto& i : b)
            cin >> i;
        sort(b.begin(), b.end());

        int g = 0;
        for (int i = 1; i < n; ++i)
            g = __gcd(g, a[i] - a[i - 1]);

        int mod = a[0] % g;
        bool ok = 1;
        for (int x : b){
            if (x % g == mod)
                ok = 0;
        }

        cout << ok << '\n';
    }
}