#include <iostream>
#include <cmath>
#include <algorithm>

using namespace::std;

bool postojiX() {
	int n, m;
	cin >> n >> m;
	long long* a = new long long[n];
	for (int i = 0; i < n; i++) cin >> a[i];
	sort(a, a + n);
	long long minraz = abs(a[1] - a[0]);
	long long pom;
	for (int i = 2; i < n; i++) {
		pom = abs(a[i] - a[i - 1]);
		if (pom < minraz) minraz = pom;
	}
	long long k = a[0] % minraz;
	bool b = true;
	for (int i = 0; i < n; i++) {
		if ((a[i] - k) % minraz != 0) {
			b = false;
			break;
		}
	}
	for (int i = 0; i < m; i++) {
		cin >> pom;
		if ((pom - k) % minraz == 0) {
			b = false;
		}
	}
	delete[] a;
	return b;
}

int main() {
	int t;
	cin >> t;
	bool d[5];
	for (int i = 0; i < t; i++) {
		d[i] = postojiX();
	}
	for (int i = 0; i < t; i++) {
		cout << d[i] << endl;
	}
	return 0;
}