#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int t, n, m;

ll ga;
bool flag;
ll a[75005];
ll b[75005];
unordered_set<ll> s;

ll gcd(ll p, ll q) {
	if (!q) return p;
	return gcd(q, p % q);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> t;
	while (t--) {
		s.clear();
		flag = 1;
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			s.insert(a[i]);
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
			if (s.find(b[i]) != s.end()) flag = 0;
		}
		if (n > 1) {
			sort(a, a + n);
			sort(b, b + m);
			ga = a[1] - a[0];
			for (int i = 1; i < n - 1; i++) {
				ga = gcd(a[i + 1] - a[i], ga);
			}
			for (int i = 0; i < m && flag; i++) {
				if (abs(b[i] - a[0]) % ga) continue;
				flag = 0;
			}
		}
		cout << flag << '\n';
	}
}