#include <iostream>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main(){
	long long i,j,m,n,t,min=9223372036854775807;
	cin>>t; 
	vector <long long> g(t);
	for(i=0;i<t;i++) g[i]=1;
	for(i=0;i<t;i++){
	
		cin>>n>>m;
		vector <long long> a(n);
		vector <long long> b(m);
		vector <long long> c(n);
		for(j=0;j<n;j++){
			cin>>a[j];}
		for(j=0;j<m;j++){
			cin>>b[j];}
		sort(a.begin(),a.end());
		for (j=0;j<n;j++){
			c[j]=a[j]-a[0];
		}
		for (j=1;j<n+1;j++){
			if(c[j]-c[j-1]<min)min=c[j]-c[j-1];
		}
		for (j=0;j<m;j++){
			if((b[j]-a[0])%min==0){
				g[i]=0;
				break;
				
			}
			
		}
		
	}
	for(i=0;i<t;i++) cout<<g[i];
}