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

using namespace std;

#define pb push_back
#define x first
#define y second
#define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

ll nzd(ll a, ll b) {
    if (!b) return a; else return nzd(b, a % b);
}

int main()
{
    IOS;
    ll t;
    cin >> t;
    while (t--) {
        ll n, m;
        cin >> n >> m;
        vector<ll> a(n);
        for (ll i = 0; i < n; i++) cin >> a[i];
        sort(a.begin(), a.end());
        ll gcd = a[1] - a[0];
        for (ll i = 2; i < n; i++) {
            gcd = nzd(gcd, a[i] - a[i - 1]);
        }
        ll ans = 1;
        for (ll i = 0, cur; i < m; i++) {
            cin >> cur;
            if (cur % gcd == a[0] % gcd) ans = 0;
        }
        cout << ans << endl;
    }
    return 0;
}