#include<iostream>
#include<cmath>
using namespace std;

unsigned long long a[75000],a1[75000],b1[75000],b[75000];
int tacno[75000];

int Euklidov(int a,int b){
	
if(a%b)	
return Euklidov(b,a%b);
else
return b;
	
}

int main(){
	
	unsigned long long i,m,n,j,p,q, min;
	
	
	cin >> n;
	
	for(i=0;i<n;i++){
		
		cin >> p >> q;		
		
		cin >> a[0];
		min = a[0];
		
		for(j=1;j<p;j++){
			
			cin >> a[j];
	
			if (a[j] < min)
				min = a[j];
				
			a1[j-1] = a[j] < a[j-1] ? a[j-1] - a[j] : a[j] - a[j-1];

		}

		long long nzd1=a1[0];

		for(j=1;j<p-1;j++){
			nzd1=Euklidov(nzd1,a1[j]);
		}
		
		tacno [i] = nzd1 != 1 ? 1 : 0;

			for(j=0;j<q;j++){
	
				cin >> b[j];

				if ((b[j]-min)%nzd1 == 0) {
					tacno [i] = 0;
				}
			}

	}
	
	for(i=0;i<n;i++)
		cout << tacno [i] << '\n';
	
	return 0;	
	}