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

int main(){
	ios_base::sync_with_stdio(false);
	
	int t,n,m,a[75000],b[75000],diff;
	bool f;
	
	cin>>t;
	
	for(int i=0;i<t;i++)
	{
		f = false;
		
		cin>>n>>m;
		for(int j=0;j<n;j++)
			cin>>a[j];
		for(int j=0;j<m;j++)
			cin>>b[j];
		
		sort(a, &(a[n]));
		
		diff = a[1] - a[0];
		
		for(int j=1;j<n-1;j++)
			if(a[j+1] - a[j] < diff)
				diff = a[j+1] - a[j];
		
		for(int j=0;j<n-1;j++)
			if((a[j+1] - a[j]) % diff != 0)
				f = true;
		
		for(int j=0;j<m;j++)
			if((b[j] - a[0]) % diff == 0)
				f = true;
		
		if(f)
			cout<<0<<endl;
		else
			cout<<1<<endl;
	}
	
	return 0;
}