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

using namespace std;

long long int findMinDiff(long long int arr[], int n) 
{ 
   sort(arr, arr+n); 
  
   long long int diff = LLONG_MAX; 
  
   for (int i=0; i<n-1; i++) 
      if (arr[i+1] - arr[i] < diff) 
          diff = arr[i+1] - arr[i]; 
          
   return diff; 
} 

main()
{
	int n, m, t;
	bool tacno = false;
	scanf("%d", &t);
	int rez[t];
	for(int br = 0; br < t; br++)
	{
		tacno = false;
		scanf("%d%d",&n, &m);
		long long int bile[n];
		for(int i = 0; i < n; i++)
		{
			scanf("%lld",&bile[i]);
		}
		
		long long int nisu[m];
		for(int i = 0; i < m; i++)
		{
			scanf("%lld",&nisu[i]);
		}
		
		long long int razlika = findMinDiff(bile, n);
		long long int a;
		for(int i = 1; i < n; i++)
		{
			a = bile[i] - bile[0];
			if(a % razlika != 0)
			{
				rez[br] = 0;
				tacno = true;
				break;
			}
		}
		if(tacno)
		{
			continue;
		}
		for(int i = 0; i < m; i++)
		{
			a = nisu[i] - bile[0];
			if(a % razlika == 0)
			{
				rez[br] = 0;
				tacno = true;
				break;
			}
		}
		if(tacno)
		{
			continue;
		}
		rez[br] = 1;
	}
	for(int br = 0; br < t; br++)
		printf("%d\n",rez[br]);
}