#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

static long long int a[75000];

long long int gcdF(long long int a, long long int b)
{
	if (b == 0) return a;
	a %= b;
	return gcdF(b, a);
}

long long int aps(long long int x) {
	if (x < 0)return -x;
	return x;
}
int compare(const void* a, const void* b)
{
	const long long int* x = (long long int*)a;
	const long long  int* y = (long long int*)b;

	if (*x > * y)
		return 1;
	else if (*x < *y)
		return -1;

	return 0;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
    int t;
	cin >> t;
	for (int i = 0; i < t; i++) {

		long long int gcd = 1;
		int n, m;
		cin >> n >> m;

		for (int i = 0; i < n; i++) 
			cin >> a[i];

		qsort(a, n, sizeof(long long int), compare);

		for (int i = 1; i < n; i++)
		{
			if (i == 1)gcd = a[1] - a[0];
			else
				gcd = gcdF(gcd, a[i] - a[i - 1]);
		}
		bool check = true;

		for (int i = 0; i < m; i++) {

			long long int x;
			cin >> x;

			if (aps(x - a[0]) % gcd == 0) 
				check = false;

		}

			if (!check)cout << 0 << "\n";
			else
				cout << 1 << "\n";

	}

}