using System;


namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] o = new int[t];
            for (int i = 0; i < t; i++)
            {
                String[] a = Console.ReadLine().Split();
                long n1 = long.Parse(a[0]);
                long m1 = long.Parse(a[1]); 
                long[] n = new long[n1];
                long[] m = new long[m1];
                String[] a1 = Console.ReadLine().Split();
                for (int j = 0; j < n1; j++)
                {
                    n[j] = long.Parse(a1[j]);
                }
                String[] a2 = Console.ReadLine().Split();
                for (int j = 0; j < m1; j++)
                {
                    m[j] = long.Parse(a2[j]);
                }
                Array.Sort(n);
                long x = n[1] - n[0];
                bool istina = true;
                for (int j = 1; j < n1; j++)
                {
                    if ((n[j] - n[j - 1]) % x != 0)
                        istina = false;
                }
                long ostatak = n[0] % x;
                for (int j = 0; j < m1; j++)
                {
                    if (m[j] % x == ostatak)
                        istina = false;
                }
                if (istina == true)
                    o[i] = 1;
                else
                    o[i] = 0;
            }
            for (int i = 0; i < t; i++)
                Console.WriteLine(o[i]);
        }
    }
}