#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int nzd(long long a, long long b)
{
	long long q = max(a, b) / min(a, b);
	long long r = max(a, b) % min(a, b);
	if (r == 0)
		return b;
	else
		return nzd(min(a, b), r);
}

int main()
{
	int t; cin >> t;
	vector<int>odgovori;
	for (int k = 0; k < t; k++)
	{
		int n, m; cin >> n >> m;
		vector<long long> prihvacene(n), neprihvacene(m);
		for (int i = 0; i < n; i++)
			cin >> prihvacene[i];
		for (int i = 0; i < m; i++)
			cin >> neprihvacene[i];
		sort(prihvacene.begin(), prihvacene.end());
		sort(neprihvacene.begin(), neprihvacene.end());

		bool tacan = true;
		long long trenutno_x = prihvacene[1] - prihvacene[0];
		for (int i = 2; i < n && tacan; i++)
		{
			long long novo_x = prihvacene[i] - prihvacene[i - 1];
			trenutno_x = nzd(novo_x, trenutno_x);
		}

		for (int i = 0; i < m && tacan; i++)
		{
			if (abs(prihvacene[0] - neprihvacene[i]) % trenutno_x == 0)
				tacan = false;
		}

		if (tacan)
			odgovori.push_back(1);
		else
			odgovori.push_back(0);
	}

	for (int odgovor : odgovori)
		cout << odgovor << endl;
}