#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 = false;
	for (int j = 1; j <= x; j++) {
		postoji = true;
		for (int i = 0; i < n; i++) {
			if (abs(a[i] - min) % j != 0) {
				postoji = false;
				break;
			}
		}
		for (int i = 0; i < m; i++) {
			if ((b[i] - min) % j == 0) {
				postoji = false;
				break;
			}
		}
		if (postoji)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 = 1; i < n; i++) {
			if ((a[i] - a[i - 1] > x) || i == 1)x = a[i] - a[i - 1];
		}
		for (int i = 1; i < m; i++) {
			if (b[i] - b[i - 1] > x)x = b[i] - b[i - 1];
		}
		if (a[0] <= b[0])postoji = postoji_f(a[0], x, n, m, a, b);
		vt[vt.size() - t - 1] = postoji;
	}
	for (int i = 0; i < vt.size(); i++) {
		cout << vt[i] << endl;
	}
	return 0;
}