#include <iostream>
#include <vector>
#include <cstdint>

static inline int64_t nzd(int64_t a, int64_t b)
{
	while (a != 0)
	{		
		if (a < b)
		{
			a = a ^ b;
			b = b ^ a;
			a = a ^ b;
		}
		a -= b;
	}
	return b;
}

int main()
{
	int takmicari;
	bool tacnost[5]{ true, true, true, true, true };
	std::cin >> takmicari;	

	for (int t = 0; t < takmicari; ++t)
	{
		int n, m;
		std::cin >> n >> m;		

		if (n == 1)
		{
			int64_t temp;
			std::cin >> temp;
			for (int i = 0; i < m; ++i)
				std::cin >> temp;
			tacnost[t] = false;
		}

		int64_t delilac, temp1, temp2, offset;
		std::cin >> temp1 >> temp2;
		delilac = abs(temp1 - temp2);
		temp1 = temp2;

		for (int i = 2; i < n; ++i)
		{
			std::cin >> temp2;
			delilac = nzd(delilac, abs(temp2 - temp1));

			if (delilac == 1)
			{
				for (int j = i + 1; j < n; ++j)				
					std::cin >> temp1;				
				tacnost[t] = false;
				goto sledeci_takmicar;
			}

			temp1 = temp2;
		}

		offset = temp1 % delilac;

		for (int i = 0; i < m; ++i)
		{
			std::cin >> temp1;
			if ((temp1 - offset) % delilac == 0)
			{
				for (int j = i + 1; j < m; ++j)
					std::cin >> temp1;
				tacnost[t] = false;
				goto sledeci_takmicar;
			}
		}

	sledeci_takmicar:;
	}


	for (int i = 0; i < takmicari; ++i)
		std::cout << tacnost[i] << ' ';

	return 0;
}