using System;

namespace zadatak_3_21._11._2020
{
    class Program
    {
        static void z(ref long a, ref long b)
        { 
            long p = a;
            a = b;
            b = p; 
        }
        static long pd(long[] a, long l, long d)
        {
            long i = l, i1 = d;
            while (i < i1)
            {
                while (i <= i1 && a[i] <= a[l]) i++;
                while (i <= i1 && a[i1] >= a[l]) i1--;
                if (i < i1) z(ref a[i], ref a[i1]);
            }
            z(ref a[l], ref a[i1]);
            return i1;
        }
        static void qs(long[] a, long l, long d)
        {
            if (l <= d)
            {
                long k = pd(a, l, d);
                qs(a, l, k - 1);
                qs(a, k + 1, d);
            }
        }
        static long da(long[] a,long[] b )
        {
            long x = a[1] - a[0];
            for (int i = 1; i < a.Length; i++)
            {
                if ((a[i] - a[i - 1]) % x == 0) ;
                else if (x % (a[i] - a[i - 1]) == 0) x = a[i] - a[i - 1];
                else return 0;
            }
            long y = a[0] % x;
            for(int i = 0; i < b.Length; i++) if (b[i] % x == y) return 0;
            return 1;
        }
        static void Main(string[] args)
        {
            long t = long.Parse(Console.ReadLine());
            long[] x = new long[t];
            string[] s;
            for (long i = 0; i < t; i++)
            {
                s = Console.ReadLine().Split();
                long n = long.Parse(s[0]);
                long m = long.Parse(s[1]);
                long[] aqs = new long[n];
                long[] bqs = new long[m];
                s = Console.ReadLine().Split();
                for (long i1 = 0; i1 < s.Length; i1++) aqs[i1] = long.Parse(s[i1]);
                qs(aqs, 0, n - 1);
                s = Console.ReadLine().Split();
                for (long i1 = 0; i1 < s.Length; i1++) bqs[i1] = long.Parse(s[i1]);
                qs(bqs, 0, m - 1);
                x[i] = da(aqs,bqs);
            }
            for (long i = 0; i < t; i++) Console.WriteLine(x[i]);
        }
    }
}