#include <iostream>
using namespace std;
int partition(long long* a, int start, int end)
{
	int pivot = a[end];
	int P_index = start;
	int i, t;
	for (i = start; i < end; i++)
	{
		if (a[i] <= pivot)
		{
			t = a[i];
			a[i] = a[P_index];
			a[P_index] = t;
			P_index++;
		}
	}
	t = a[end];
	a[end] = a[P_index];
	a[P_index] = t;
	return P_index;
}
void Quicksort(long long* a, int start, int end)
{
	if (start < end)
	{
		int P_index = partition(a, start, end);
		Quicksort(a, start, P_index - 1);
		Quicksort(a, P_index + 1, end);
	}
}
int main() {
	int n, g, m;
	cin >> n;
	int* t = new int[n];

	bool k;


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

		cin >> g;
		cin >> m;
		long long min = 9223372036854775807;
		//long long p[g], l[m];
		long long* p = new long long[g];
		long long* l = new long long[m];
		for (int j = 0; j < g + m; j++)
			if (j < g) {

				cin >> p[j];
			}
			else {
				cin >> l[j - g];
			}


		Quicksort(p, 0, g - 1);
		Quicksort(l, 0, m - 1);

		for (int j = 0; j < g + m; j++) {
			if (j < g - 1) {
				if (p[j + 1] - p[j] < min)
					min = p[j + 1] - p[j];

				//cout <<j<<" "<<g-1<<" ";
			}
			else {
				if ((l[j+1 - g] - p[0] - min) % min != 0) { k = true;  }
				else {
					k = false; break;
				}
			}

		}
		if (k == true) {
			t[i] = 1;
		}
		else {
			t[i] = 0;
		}
		k = true;
		
	}
	for (int i = 0; i < n - 1; i++)cout << t[i] << endl;
	cout << t[n - 1];
}