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

long long nzd(long long a, long long b)
{
	while (a > 0)
	{
		int temp = a;
		a = b % a;
		b = temp;
	}

	return b;
}

long long nzd_n(const vector<long long>& v)
{
	long long trenutni_nzd = 0;
	for (long long a : v)
	{
		trenutni_nzd = nzd(trenutni_nzd, a);
	}

	return trenutni_nzd;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t; cin >> t;
	while (t--)
	{
		int n, m; cin >> n >> m;
		vector<long long> a(n);
		for (int i = 0; i < n; i++)
			cin >> a[i];
		vector<long long> b(m);
		for (int i = 0; i < m; i++)
			cin >> b[i];
		sort(a.begin(), a.end());
		sort(b.begin(), b.end());

		vector<long long> razlike_a(n - 1);
		for (int i = 1; i < n; i++)
			razlike_a[i - 1] = a[i] - a[i - 1];

		vector<long long> razlike_ab(m);
		for (int i = 0; i < m; i++)
			razlike_ab[i] = abs(a[0] - b[i]);

		long long nzd_a = nzd_n(razlike_a);

		int moze = 1;
		for (long long razlika : razlike_ab)
		{
			if (razlika % nzd_a == 0)
				moze = 0;
		}

		cout << moze << endl;
	}
}