using System;

namespace Minimalan_trud
{
    class Program
    {
        static bool provera(long[] a, long[] b)
        {
            bool t = true;
            long x = 0;
            Array.Sort(a);
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (x == 0)
                {
                    x = a[i + 1] - a[i];
                }
                else if (a[i + 1] - a[i] != x)
                {
                    long s = a[i + 1] - a[i];
                    if (s % x == 0 || x % s == 0)
                    {
                        if (x % s == 0)
                            x = s;
                    }
                    else
                        t = false;
                }
            }
            if (!t)
                return t;
            Array.Sort(b);
            long o = a[0] % x;
            for (int i = 0; i < b.Length-1; i++)
            {
                if ((b[i] - o) % x == 0)
                    t = false;
            }
            return t;
        }
        static void Main(string[] args)
        {
            int n, m;
            int t = Convert.ToInt32(Console.ReadLine());
            bool[] p = new bool[t];
            string[] k;
            for (int i = 0; i < t; i++)
            {
                k = Console.ReadLine().Split();
                n = Convert.ToInt32(k[0]);
                m = Convert.ToInt32(k[1]);
                long[] a = new long[n];
                long[] b = new long[m];
                k = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                    a[j] = Convert.ToInt64(k[j]);
                k = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                    b[j] = Convert.ToInt64(k[j]);
                p[i] = provera(a, b);
            }
            for (int i = 0; i < t; i++)
            {
                if(p[i])
                    Console.WriteLine(1);
                else
                    Console.WriteLine(0);
            }
        }
    }
}