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

vector<long long int>niz(int n) {
	vector<long long int>niz(n);
	for (int i = 0; i < n; i++) {
		cin >> niz[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (niz[j] > niz[i])swap(niz[i], niz[j]);
		}
	}
	return niz;
};

bool postoji_f(long long int min, long long int x, int n, int m, vector<long long int>a, vector<long long int>b) {
	bool postoji = true;
	for (int i = 0; i < n; i++) {
		if ((a[i] - min) % x != 0) {
			postoji = false;
			break;
		}
	}
	for (int i = 0; i < m; i++) {
		if ((b[i] - min) % x == 0) {
			postoji = false;
			break;
		}
	}
	return postoji;
}

int main() {
	int t, n, m;
	cin >> t;
	vector<bool>vt(t);
	while (t-- > 0) {
		long long int x = 0, min;
		bool postoji = true;
		cin >> n >> m;
		vector<long long int>a = niz(n);
		vector<long long int>b = niz(m);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if ((abs(a[i] = b[j]) < x) || (i == 0 && j == 0))x = abs(a[i] - b[j]);
			}
		}
		if (a[0] <= b[0])postoji = postoji_f(a[0], x, n, m, a, b);
		else postoji = postoji_f(b[0], x, m, n, b, a);
		vt[vt.size() - t - 1] = postoji;
	}
	for (int i = 0; i < vt.size(); i++) {
		cout << vt[i] << endl;
	}
	return 0;
}