#include <bits/stdc++.h>

using namespace std;

int MAX = 75000;
long long A[75000];
long long B[75000];
vector<long long> RazlA(75000);
vector<long long> RazlB(75000);
	
bool NZD(long long a, long long b)
{
	while (b != 0)
	{
		long long temp = b;
		b = a % b;
		a = temp;
	}
	return a;
}

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

int main()
{
	int T;
	cin >> T;
	//long long A[75000];
	//long long B[75000];
	//vector<long long> RazlA(75000);
	//vector<long long> RazlB(75000);
	bool resenja[T];
	int minR = 0;
	// ako je nzd niza A jednak
	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;
		}
		cin >> b;
		B[0] = b;
		for (int k = 1; k < m; k++)
		{
			cin >> b;
			B[k] = b;
			RazlB[k-1] = B[k] - B[k-1];
		}
		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(n, m);
		}
	}
	for (int i = 0; i < T; i++)
	{
		cout << resenja[i] << "\n";
	}
	return 0;
}