#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>


using namespace std ;


int main()
{
	long long t, n, m, uvazene, odbijene, raz, br ;
	vector<long long>u ;
	vector<long long>o ;
	vector<long long >ispis;
	
	cin>>t;
	for (long long i=0; i <t ; i++)
	{
		cin>>n>>m;
		for (long long j=0; j<n; j++)
		{
			cin>>uvazene;
			u.push_back(uvazene); 
		}
		sort(u.begin(), u.end());
		for (long long h = 0; h<m; h++)
		{
			cin>>odbijene;
			o.push_back(odbijene);
		}
		sort(o.begin(),o.end());
		raz = u[1]-u[0];
		for (long long k = 1; k!= u.size(); k++)
		{
			raz = min(raz,(u[k]-u[k-1]));
		}
		for (long long l=1; l<u.size();l++)
		{
			if((u[l]-u[l-1])%raz==0)
			{
				br = 1;
			}
			else
			{
				br = 0 ;
				break;
			}	
		}
		if (br==1)
		{
			for (long long p= 0; p<o.size();p++)
			{
				if (abs(u[0]-o[p])%raz != 0)
				{
					br = 1;
				}
				else
				{
					br = 0;
					break;	
				}
			}
		}
		ispis.push_back(br);
		u.clear();
		o.clear();
	}
	for (long long kraj = 0; kraj<ispis.size(); kraj++)
	{
		cout<<ispis[kraj]<<"\n";
	}
	
	
	return 0;
}