#include<bits/stdc++.h>
using namespace std;
unsigned long long raz(unsigned long long a, unsigned long long b){
	if(a>b)return a-b;
	else return b-a;
}
struct niz{
	unsigned long long n;
	unsigned long long m;
	vector<unsigned long long> a;
	vector<unsigned long long> b;
};
int main(){
	unsigned long long t;
	cin>>t;
	niz k[t];
	unsigned long long x;
	for(unsigned long long i=0;i<t;i++){
		cin>>k[i].n>>k[i].m;
		for(unsigned long long j=0;j<k[i].n;j++){
			cin>>x;
			k[i].a.push_back(x);
		}
		for(unsigned long long j=0;j<k[i].m;j++){
			cin>>x;
			k[i].b.push_back(x);
		}
	}
	for(unsigned long long i=0;i<k[i].n;i++){
			int pom[k[i].a.size()];
			int n1=k[i].a.size();
			for(int j=0;j<n1;j++)pom[j]=k[i].a[j];
			sort(pom,pom+n1);
			for(int j=0;j<n1;j++)k[i].a[j]=pom[j];
	}
	int res[t];
	for(unsigned long long i=0;i<t;i++){
		res[i]=1;
		x=k[i].a[1]-k[i].a[0];
		for(unsigned long long j=1;j<k[i].n-1;j++){
			if((k[i].a[j+1]-k[i].a[j])%x!=0){
				res[i]=0;
				break;
			}
		}
		for(unsigned long long j=0;j<k[i].m&&res[i]==1;j++){
			if(raz(k[i].a[0],k[i].b[j])%x==0){
				res[i]=0;
				break;
			}
		}
	}
	for(unsigned long long i=0;i<t;i++)cout<<res[i]<<endl;
}