#include <iostream>
#include <math.h>
#define ll long long
using namespace std;
ll nzd(ll a, ll b)
{
	if(!a) return b;
	return nzd(b % a, a);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	int n, m;
	ll a, b, d, r;
	bool res;
	cin >> t;
	while(t--)
	{
		cin >> n >> m;
		cin >> a;
		d = 0;
		res = 1;
		for(int i = 1; i < n; i++)
		{
			cin >> b;
			d = nzd(min(d,a-b),max(d,a-b));
			b = a;
		}
		r = b % d;
		for(int i = 0; i < m; i++)
		{
			cin >> b;
			res &= !(b % d == r);
		}
		cout << res << "\n";
	}
}