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

int main()
{
	long long k,u, razmak; 
	int t, rez, rez1; 
	long n,m;
	cin>>t;
	vector<long long> v;
	vector<long long> v1;
	vector<int>ispis;
	
	for (int i=0; i<t; i ++)
	{ 
		
		cin>>n>>m;
		for (int j = 0; j<n ; j++)
		{
			
			cin>>k;
			v.push_back(k);	
		}
		sort(v.begin(), v.end());
		
		
		for (int l = 0 ; l<m; l++)
		{
		
			cin>>u;
			v1.push_back(u);
		}
		
		sort(v1.begin(),v1.end());
	 	razmak = v[1]-v[0] ;
		
		for (int y = 0 ; y<n ; n++)
		{
			if (v[y+1]-v[y]==razmak)
			{
				rez = 1;
			}
			else
			{
				rez = 0;
			}
		}
		
		for (int x = 0 ; x<m ; x++)
		{
			if (v1[x+1]-v1[x]!=razmak)
			{
				rez1 = 1;	
			}
			else
			{
				rez1 = 0;	
			}
		
		}
		if (rez1 == 1 and rez==1)
		{
			ispis.push_back(1);
		}
		else
		{
			ispis.push_back(0);
		}
	}
	for (int r = 0 ;r<t-1; r++)
	{
		cout<<ispis[r]<<"\n";
	}
	cout<<ispis[t-1];
	return 0;
}