#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(int a, int b)
{
	while (b != 0)
	{
		int temp = b;
		b = a % b;
		a = temp;
	}
	return a;
}

bool postoji(int n, int m)
{
	
	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];
	// 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] = A[j] - A[j-1];
		}
		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)
		{
			int raz = abs(A[1]-A[0]);
			for (int k = 0; k < m; k++)
			{
				int prvi = (A[0]-B[k])%raz;
				int 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;
}