#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll absolutna(ll x) { if (x < 0) return (-1*x); else return x; }
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int main()
{
    int t; scanf("%d", &t);

    while (t--) {
        int n, m; scanf("%d %d", &n, &m);
        ll a[n], b[m];
        for (int i = 0; i < n; ++i) scanf("%lld", &a[i]);
        for (int i = 0; i < m; ++i) scanf("%lld", &b[i]);

        vector<ll> razl;
        for (int i = 1; i < n; ++i) razl.push_back(absolutna(a[i]-a[i-1]));
        ll step = *min_element(begin(razl), end(razl));

        // cout << step << endl;
        ll prvaGod = *min_element(a, a+n);
        bool sol = true;
        for (int i = 0; i < m; ++i) {
            if ((b[i] - prvaGod) > 0 && (b[i] - prvaGod) % step == 0) sol = false;
        }

        if (sol == true) printf("1\n");
        else printf("0\n");
    }

    return 0;
}