#include <bits/stdc++.h>
using namespace std;
unsigned long long int ucitaj()
	{
		unsigned long long int rez=0;
		char ch;
		ch=getchar();
		while (true)
		{
			if (ch>='0' or ch<='9')break;
			ch=getchar();
		}
		rez=(ch-'0');
		while (true)
		{
			ch=getchar();
			if (ch<'0' or ch>'9') break;
			rez=rez*10+(ch-'0');
		}
		return rez;
	}
/*	int ucitaj()
	{
		bool minus=false;
		int rez=0;
		char ch;
		ch=getchar();
		while (true)
		{
			if (ch=='-') break;
			if (ch>='0' or ch<='9')break;
			ch=getchar();
		}
		if (ch=='-') minus=true; else rez=(ch-'0');
		while (true)
		{
			ch=getchar();
			if (ch<'0' or ch>'9') break;
			rez=rez*10+(ch-'0');
		}
		if (minus)
		{
			return -rez;
		}
		else
		{
			return rez;
		}
	}*/
int main()
{
	//int ;
	unsigned long long int A[75001],B[75001],min,t,n,m,g,C[6],e,q,j,i,k,p,pr,D[1000];
	e=0;
	q=0;
	t=ucitaj();
	for (i=0;i<t;i++)
	{
		p=1;
		pr=1;
		n=ucitaj();
		m=ucitaj();
		for (j=0;j<n;j++)
		{
			A[j]=ucitaj();
			//cin>>A[j];
			cout<<A[j]<<endl;
		}
		for (j=0;j<m;j++)
		{
			//B[j]=0;
			B[j]=ucitaj();
			cout<<B[j]<<endl;
		}
		sort(A,A+n); 
		min=A[1]-A[0];    //PROVERITI AKO BUDE PROBLEMA
		for (j=1;j<n-1;j++)
		{
			if (A[j+1]-A[j]<min) 
			{
				min=A[j+1]-A[j];
			}
		}
		if (min==1)
		{
			C[e]=0;
			e++;
			goto dole;
		}
		D[q]=min;
		q++;
		for (j=2;j<=min/j;j++)
		{
		 	while (min%j==0)
			{
 	 		 	  min=min/j;
 	 		 	  D[q]=min;
 	 		 	  q++;
				  p=j;
				  if(p!=pr)
				  {
				  	pr=p;
				  	if (m>n)
				  	{
				  		for (k=0;k<n-1;k++)
				  		{
				  		 	if(((A[k]-A[k+1])%p)!=0) 	
				  		 	{
				  		 		goto sle;///NEXT p
							}
				  		}
				  		for (k=0;k<m;k++)
				  		{
				  		 	if(((B[k]-A[0])%p)==0)
							   {
	  						   		goto sle;///NEXT p			  
							   }	
		   				}
		   				 C[e]=1;
		   				 e++;
						 goto dole;///NEXT i
			  		}
			  		else
			  		{
			  			for (k=0;k<m;k++)
				  		{
				  		 	if(((B[k]-A[0])%p)==0)
							   {
	  						   		goto sle;///NEXT p			  
							   }	
		   				}
			  		 	for (k=0;k<n-1;k++)
				  		{
				  		 	if(((A[k]-A[k+1])%p)!=0) 	
				  		 	{
				  		 		goto sle;///NEXT p
							}
				  		}
		   				 C[e]=1; 
		   				 e++;
						 goto dole;///NEXT i	  
		  		    }
				  }
				  sle:
				  	g=0;
            }
	    }
		if(min>1)
		{
  		 	   p=min;
  		 	   if(p!=pr)
				  {
				  	pr=p;
				  	if (m>n)
				  	{
				  		for (k=0;k<n-1;k++)
				  		{
				  		 	if(((A[k]-A[k+1])%p)!=0) 	
				  		 	{
				  		 		
				  		 		goto sledpr;
							}
				  		}
				  		for (k=0;k<m;k++)
				  		{
				  		 	if(((B[k]-A[0])%p)==0)
							   {			  						  
	  						   		goto sledpr;			  
							   }	
		   				}
		   				 C[e]=1;
		   				 e++;
						 goto dole;///NEXT i
			  		}
			  		else
			  		{
			  			for (k=0;k<m;k++)
				  		{
				  		 	if(((B[k]-A[0])%p)==0)
							   {
	  						   		goto sledpr;			  
							   }	
		   				}
			  		 	for (k=0;k<n-1;k++)
				  		{
				  		 	if(((A[k]-A[k+1])%p)!=0) 	
				  		 	{
				  		 		goto sledpr;
							}
				  		}
		   				 C[e]=1; 
		   				 e++;
						 goto dole;///NEXT i	  
		  		    }
				  }	   
		}
		
		sledpr:
		for (j=0;j<q;j++)
	    {
		  if (m>n)
				  	{
				  		for (k=0;k<n-1;k++)
				  		{
				  		 	if(((A[k]-A[k+1])%D[j])!=0) 	
				  		 	{
				  		 		
				  		 		goto nepr;
							}
				  		}
				  		for (k=0;k<m;k++)
				  		{
				  		 	if(((B[k]-A[0])%D[j])==0)
							   {			  						  
	  						   		goto nepr;			  
							   }	
		   				}
		   				 C[e]=1;
		   				 e++;
						 goto dole;///NEXT i
			  		}
			  		else
			  		{
			  			for (k=0;k<m;k++)
				  		{
				  		 	if(((B[k]-A[0])%D[j])==0)
							   {
	  						   		goto nepr;			  
							   }	
		   				}
			  		 	for (k=0;k<n-1;k++)
				  		{
				  		 	if(((A[k]-A[k+1])%D[j])!=0) 	
				  		 	{
				  		 		goto nepr;
							}
				  		}
		   				 C[e]=1; 
		   				 e++;
						 goto dole;///NEXT i	  
		  		    }
		  		    nepr:
		  		    	g=0;
		}
	    C[e]=0;
	    e++;
		dole:
			g=0;
    }
	
	
	for (i=0;i<t;i++)
	{
		//printf("%d\n",C[i]);
		cout<<C[i]<<endl;
	}
}