#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int res[10];
long long int nzd(long long int a,long long int b)
{
	if(a<b) { long long int p=a;a=b;b=p; }
	if(b==0) return a;
	a=a%b;
	return nzd(b,a);
}
int main(int argc, char** argv) {
	int t;cin>>t;
	for(int slj=0;slj<t;slj++)
	{
		
		int n,m;cin>>n>>m;long long nzd1,ostatak;
		if(n==2) { long long int x,y;cin>>x>>y; nzd1=abs(x-y);ostatak=x%nzd1;}
		else {
			long long int primer1;
			long long int x,y,trenutninzd; cin>>x>>y; trenutninzd=abs(x-y); long long int z,razlika2;
			for(int i=2;i<n;i++)
			{
				cin>>z; razlika2=abs(z-y); trenutninzd=nzd(trenutninzd,razlika2); x=y;y=z;
			}
			ostatak=z%trenutninzd;
			nzd1=trenutninzd;
		}
		
		if(nzd1==1) {res[slj]=0; continue;	}
		bool jelistiostatak=false;
		long long int oks;
		for(int i=0;i<m;i++)
		{
			cin>>oks; if(oks%nzd1==ostatak) { res[slj]=0; jelistiostatak=true;	}
		}
		if(!jelistiostatak) res[slj]=1;
		
	}
	for(int i=0;i<t;i++) cout<<res[i]<<endl;
	return 0;
}