using System;

namespace ConsoleApp4
{
	class Program
	{
		private static int nzd(int a, int b)
		{


			while (b != 0)
			{
				int t;
				t = b;
				b = a % b;
				a = t;
			}
			return a;
		}


		static void Main(string[] args)
		{

			int br = Convert.ToInt32(Console.ReadLine());
			int[] arr = new int[14];
			for (int m = 0; m < br; m++)
			{
				string poc = Console.ReadLine();
				int poz = poc.IndexOf(" ");
				int br1 = Convert.ToInt32(poc.Substring(0, poz));
				int br2 = Convert.ToInt32(poc.Substring(poz, poz + 1));
				int p = br1;
				int n = br2;

				int[] a = new int[] { 0,0,0,0,0,0,0,0};
				int[] b = new int[] { 75000,0,0,0,0,0,0,0,0,0,0,0 };
				string poc1 = Console.ReadLine();
				int poz1 = poc.IndexOf(" ");
				int br12 = Convert.ToInt32(poc1.Substring(0, poz1));
				int br22 = Convert.ToInt32(poc1.Substring(poz1, poz1 + 1));
				 a[0]= br12;
				 a[1] = br22;
				

				int rsk = Math.Abs(a[1] - a[0]);
				for (int i = 2; i < p; i++)
				{
					string poc2 = Console.ReadLine();
					int poz12 = poc2.IndexOf(" ");
					int br122 = Convert.ToInt32(poc2.Substring(0, poz12));

					a[i] = br122;
					int rzm = Math.Abs(a[i] - a[i - 1]);
					if (nzd(rzm, rsk) > 1)
						rsk = nzd(rzm, rsk);
					if (rzm % rsk != 0)
					{
						arr[m] = 0;
						break;
					}

				}
				

				for (int i = 1; i < n; i++)
				{
					string poc3 = Console.ReadLine();
					int poz3 = poc3.IndexOf(" ");
					int br3 = Convert.ToInt32(poc3.Substring(0, poz3));
					b[i] = br3;
					int rzm = Math.Abs(b[i] - b[i - 1]);
					if (rzm % rsk == 0)
					{
						arr[m] = 0;
						break;
					}
				}

			}
			for (int i = 0; i < br; i++)
			{
				Console.WriteLine(arr[i]);
			}


		}
	}
}