#include<bits/stdc++.h>

using namespace std;

int t, n, m, pom=1, s[10];
long long a[75005], b, x, mini;

long long nzd(long long k1, long long k2) {
	long long qq;
	while(1==1)
	{
	qq=k2;
	k2=k1%k2;
	k1=qq;
	if (k2 == 0) return k1;
	}
	return 1;
}

long long bs() {
	long long t;
	for (int j=2;j<n;j++) {
		t=a[j] - a[j-1];
		if((t>mini)&&(t%mini==0)) break;
		if((t<=mini)&&(mini%t==0)) {mini = t; break;}
		mini=nzd(t,mini);
		if (mini == 1) {mini = -1; break;}
	}
	return mini;
}

int check() {
	if ((abs(b-a[0]) % x) == 0) return -1; else return 1;
}

int main()
{
	scanf("%d",&t);
	
	for (int i=1;i<=t;i++) {
		scanf("%d%d",&n,&m);
		for (int j=0;j<n;j++)
		 scanf("%lld",&a[j]);
		
		sort(a,a+n);
		mini=a[1] - a[0];
		
		x=bs();
		if (x == -1) { pom=-1; continue; }
		
		for (int j=0;j<m;j++) {
			scanf("%lld",&b);
			if (pom == -1) continue;
			pom = check();
		}
		if (pom == -1) s[i]=0; else s[i]=1;
		pom = 1;
	}
	for (int i=1;i<=t;i++)
	 if (i<t) printf("%d\n",s[i]); else printf("%d",s[i]);
}