using System;

namespace PetljaTak
{
    class Program
    {       
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] res = new int[t];
            for (int j = 0; j < t; j++)
            {
                string[] s = Console.ReadLine().Split();
                
                int n = int.Parse(s[0]);
                int m = int.Parse(s[1]);

                ulong[] a = new ulong[n];
                ulong[] b = new ulong[m];
                s = Console.ReadLine().Split();
                for (int i = 0; i < n; i++)
                {
                    a[i] = ulong.Parse(s[i]);
                }
                s = Console.ReadLine().Split();
                for (int i = 0; i < m; i++)
                {
                    b[i] = ulong.Parse(s[i]);
                }
                Array.Sort(a);
                Array.Sort(b);
                ulong x = 0;
                x = a[1] - a[0];
                int r = -1;
                for (int i = 0; i < n - 1; i++)
                {
                    if ((a[i + 1] - a[i]) % x != 0)
                    {
                        r = 0;
                        break;
                    }

                }
                for (int i = 0; i < m; i++)
                {
                    if ((b[i] - a[0]) % x == 0)
                    {
                        r = 0;
                    }
                    else
                    {
                        r = 1;
                        break;
                    }
                }
                res[j] = r;

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


        }
    }
}