#include <bits/stdc++.h>

using namespace std;

int MAX = 75000;
long long A[75000];
long long B[75000];
vector<long long> RazlA(75000);

bool postoji(long long minRaz, int n, int m)
{
	for (int k = 0; k < m; k++)
	{
		long long x = abs((A[0]-B[k]))%minRaz;
		if (x == 0) return 0;
	}
	return 1;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int T;
	cin >> T;
	bool resenja[T];
	long long minR = 0;
	for (int i = 0; i < T; i++)
	{
		int n, m;
		cin >> n;
		cin >> m;
		long long a, b;
		cin >> a;
		A[0] = a;
		for (int j = 1; j < n; j++)
		{
			cin >> a;
			A[j] = a;
			RazlA[j-1] = abs(A[j] - A[j-1]);
		}
		minR = RazlA[0];
		for (int j = 1; j < n-1; j++)
		{
			if (RazlA[j] < minR) minR = RazlA[j];
		}
		cin >> b;
		B[0] = b;
		for (int k = 1; k < m; k++)
		{
			cin >> b;
			B[k] = b;
		}
		if (n == 2)
		{
			long long raz = abs(A[1]-A[0]);
			for (int k = 0; k < m; k++)
			{
				long long prvi = (A[0]-B[k])%raz;
				long long drugi = (A[1]-B[k]%raz);
				if (prvi == 0 || drugi == 0)
				{
					resenja[i] = 0;
				}
			}
			resenja[i] = 1;
		}
		else
		{
			resenja[i] = postoji(minR, n, m);
		}
	}
	for (int i = 0; i < T; i++)
	{
		cout << resenja[i] << "\n";
	}
	return 0;
}