using System;

namespace kv1_z3
{
    class Program
    {
        struct godine 
        {
            public long[] prihvacene;
            public long[] neprihvacene;
            public long p;
            public long n;
        }
        static void Main(string[] args)
        {
            long t = long.Parse(Console.ReadLine());
            string[] s;
            godine[] a = new godine[t];
            for(long i = 0; i < t; i++)
            {
                s = Console.ReadLine().Split();
                a[i].p = long.Parse(s[0]);
                a[i].n = long.Parse(s[1]);
                s = Console.ReadLine().Split();
                a[i].prihvacene = new long[a[i].p];
                for (long j = 0; j < a[i].p; j++) a[i].prihvacene[j] = long.Parse(s[j]); 
                s = Console.ReadLine().Split();
                a[i].neprihvacene = new long[a[i].n];
                for (long r = 0; r < a[i].n; r++) a[i].neprihvacene[r] = long.Parse(s[r]);
            }
            for (long i = 0; i < t; i++)
            {
                bool x = true;
                long minrazlika = Math.Abs(a[i].prihvacene[0] - a[i].prihvacene[1]);
                for (long j = 2; j < a[i].p; j++) if (Math.Abs(a[i].prihvacene[j - 1] - a[i].prihvacene[j]) < minrazlika) minrazlika = Math.Abs(a[i].prihvacene[j - 1] - a[i].prihvacene[j]);
                long prviostatak = a[i].prihvacene[0] % minrazlika;
                for (long j = 0; j < a[i].p; j++) if (a[i].prihvacene[j] % minrazlika != prviostatak) x = false;
                for (long j = 0; j < a[i].n; j++) if (a[i].neprihvacene[j] % minrazlika == prviostatak) x = false;
                if (x) Console.WriteLine("1");
                else Console.WriteLine("0");
            }
        }
    }
}