#include <iostream>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) 
{
	int n1, m, n ;
	cin>>n1;
	int l;
	long long r = 0;
	int k=1;
	for (int i=0; i<n1; i++)
	{
		cin>>n>>m;
		long long a[n];
		for (int j=0; j<n; j++)
		{
		cin>>a[j];	
			
		}
		sort (a, a+n);
		for (int j=0; j<n;j++)
		{
			if (j==0)
			{
			r=	a[j+1]-a[j];
			}
		if (a[j+1]-a[j] < r)
		{
		r= 	a[j+1]-a[j];
		}	
			
		}
		long long b[m];
		for (int j=0; j<m; j++)
		{
		cin>>b[j];	
		}
		sort (b, b+m);
		
		while (a[0]<b[m-1])
		{
		if (a[0]==(b[m-1]-r))
		{
		k=0;	
		}
		a[0]=a[0]+r;
			
		}
		cout<<k<<endl;
		k=1;
		r=0;
		
	}