using System;

namespace feladat3
{
	class Program
	{
		static int nemlehet = 0;
		public static void Main(string[] args)
		{
			int megoldasok = Convert.ToInt32(Console.ReadLine());
			int[] print = new int[megoldasok];
			for (int i = 0; i < megoldasok; i++) {
				string be = Console.ReadLine();
				string[] beT = be.Split(' ');
				int N = Convert.ToInt32(beT[0]);
				int M = Convert.ToInt32(beT[1]);
				
				be = Console.ReadLine();
				beT = be.Split(' ');
				int[] Ntomb = new int[beT.Length];
				for (int j = 0; j < beT.Length; j++) {
					Ntomb[j] = Convert.ToInt32(beT[j]);
				}
				
				be = Console.ReadLine();
				beT = be.Split(' ');
				int[] Mtomb = new int[beT.Length];
				for (int j = 0; j < beT.Length; j++) {
					Mtomb[j] = Convert.ToInt32(beT[j]);
				}
				
				int lko = 0;
				if(N==2){
					lko = LKO(Ntomb[0],Ntomb[1]);
				}else{
					lko = LKO(Math.Abs(Ntomb[0]-Ntomb[1]),Math.Abs(Ntomb[2]-Ntomb[1]));
				}
				
				nemlehet = Ntomb[0]%lko;
				
				print[i] = OR10(lko,Mtomb);
				
				
				
			}
			for (int i = 0; i < megoldasok; i++) {
				Console.WriteLine(print[i]);
			}
			
		}
		
		public static int LKO(int a, int b) 
		{ 
   			if (a == 0) 
      			return b; 
   			if (b == 0) 
      			return a; 
 
   			if (a > b) 
      			return LKO(a % b, b); 
   			else 
      			return LKO(a, b % a); 
}
	
		public static int OR10(int a,int[] b){
			for (int i = 0; i < b.Length; i++) {
				if(b[i]%a == nemlehet)
					return 0;
			}
			
			return 1;
		}
	}
}