#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void insertSorted(unsigned long long int arr[], int &n, int key){
   int i;
   for (i = n - 1; (i >= 0 && arr[i] > key); i--)
      arr[i + 1] = arr[i];
   arr[i + 1] = key;
   n = n + 1;
}
int main(int argc, char** argv) {
	int l=0;
	int t,n,m;
	unsigned long long int x;
	cin>>t;
	int rez[t];
	int br=0;
	for(int i=0;i<t;i++)
	{
		cin>>n>>m;
		unsigned long long int da[n];
		unsigned long long int ne[m];
		if(n==2)
		{
		for(int j=0;j<n;j++)
		{
			cin>>x;
			insertSorted(da,l,x);
		}
		for(int j=0;j<m;j++)
		{
			cin>>x;
			insertSorted(ne,l,x);
		}
		int r=da[1]-da[0];
		int b=0;
		for(int j=0;j<m;j++)
		{
			if(ne[j]+r!=da[0] && ne[j]+r!=da[1] && ne[j]-r!=da[0] && ne[j]-r!=da[1])
			b++;
		}
		if(b==m) rez[br]=1;
		else rez[br]=0;
		br++;
		}
	}
	for(int i=0;i<br;i++) 
	{
		if(i==br-1) cout<<rez[i];
		else cout<<rez[i]<<endl;
	}	
	return 0;
}