using System;



namespace takmicenje
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] outputs = new int[t];
            for (int i = 0; i < t; i++)
            {
                string input = Console.ReadLine();
                var d = input.Split(' ');
                int N = int.Parse(d[0]);
                int M = int.Parse(d[1]);
                long[] As = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
                long[] Bs = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);

                long[] DeltaAs = CalculateRazlike(As);
                long[] DeltaBs = CalculateRazlike(Bs);

                long ANzd = nzdArray(DeltaAs);
                long BNzd = nzdArray(DeltaBs);
                if(nzd(ANzd, BNzd) == 1)
                {
                    outputs[i] = 1;
                }
                else
                {
                    outputs[i] = 0;
                }
            }
            foreach(int output in outputs)
            {
                Console.WriteLine(output);
            }
            Console.ReadLine();

        }
        static long nzd(long a, long b)
        {
            long t;

            while (b != 0)
            {
                t = b;
                b = a % b;
                a = t;
            }
            return a;
        }
        static long nzdArray(long[] a)
        {
            long currentNzd = a[0];
            foreach(long num in a)
            {
                currentNzd = nzd(currentNzd, num);
            }
            return currentNzd;
            
        }
        static long[] CalculateRazlike(long[] b)
        {
            Array.Sort(b);
            long[] c = new long[b.Length - 1];
            for (int i = 1; i < b.Length; i++)
            {
                c[i - 1] = b[i] - b[i - 1];
            }
            return c;
        }
    }
}