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

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, m;
		cin >> n >> m;
		long long a[n], b[m];
		for (int j = 0; j < n; j++) cin >> a[j];
		for (int j = 0; j < m; j++) cin >> b[j];
		sort(a, a + n);
		long long diff = LONG_LONG_MAX;
		for (int j = 0; j < n-1; j++)
			if (a[j + 1] - a[j] < diff)
				diff = a[j + 1] - a[j];
		bool stop = false;
		for (int j = 0; j < m && !stop; j++) {
			if (b[j] > a[0] && (b[j] - a[0]) % diff == 0) {
				cout << 0 << "\n";
				stop = true;
			}
		}
		if (!stop) cout << 1 << "\n";
	}
	return 0;
}