#include <bits/stdc++.h>
#define ll long long

using namespace std;

int T;
int N;
int M;
ll a[75010];
ll b[75010];
ll X;
bool ispis[10];


ll NZD (ll a, ll b)
{
    ll c = a % b;
    while (c > 0)
    {
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> T;
	for (int i = 0; i < T; i++)
    {
        cin >> N >> M;
        ispis[i] = true;
        for (int j = 0; j < N; j++)
        {
            cin >> a[j];
            if (j == 1)
                X = abs(a[j] - a[j - 1]);
            else if (j > 1)
                X = NZD(X,abs(a[j] - a[j - 1]));
        }
        for (int j = 0; j < M; j++)
        {
            cin >> b[j];
            if ((abs(b[j] - a[0]) % X == 0) && (ispis[i]))
                ispis[i] = false;
        }
    }
    for (int i = 0; i < T; i++)
        cout << ispis[i] << endl;
	return 0;
}