#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
ll nzd(ll a, ll b) 
{
	if (b == 0)return a;
	return nzd(b, a % b);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cerr.tie(0);
	int t;
	cin >> t;
	for (; t > 0; t--) {
		int n, m;
		cin >> n >> m;
		ll p;
		cin >> p;
		ll trnz = 0;
		for (int i = 1; i < n; i++) {
			ll x;
			cin >> x;
			trnz = nzd(trnz, abs(p-x));
		}
		ll mo = p % trnz;
		bool ok = true;
		for (int i = 0; i < m; i++) {
			ll x;
			cin >> x;
			if (x % trnz == mo) ok = false;
		}
		if (ok)cout << "1\n";
		else cout << "0\n";
	}
	return 0;
}