using System;

namespace zalbe
{
    class Program
    {
        static long Aps(long n)
        {
            if (n > 0) return n;
            else return -n;
        }
        static long NZD(long a, long b)
        {
            long ost;

            while (b != 0)
            {
                ost= a % b;
                a = b;
                b = ost;
            }

            return a;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());

            int[] res = new int[t];
            for(int g = 0; g < t; g++)
            {
                string s = Console.ReadLine();
                string[] s1 = s.Split();
                int n = int.Parse(s1[0]);
                int m = int.Parse(s1[1]);

                long[] a = new long[n];
                long[] b = new long[m];

                string sa = Console.ReadLine();
                string[] sa1 = sa.Split();
                for (int i = 0; i < n; i++)
                {
                    a[i] = long.Parse(sa1[i]);
                }
                string sb = Console.ReadLine();
                string[] sb1 = sb.Split();
                for (int i = 0; i < m; i++)
                {
                    b[i] = long.Parse(sb1[i]);
                }

                long x = Aps(a[0] - a[1]);
                long r;
                for(int i = 1; i < n-1; i++)
                {
                    r = Aps(a[i] - a[i + 1]);
                    x = NZD(x, r);
                }
                bool p = true;
                for(int i = 0; i < m; i++)
                {
                    if ((b[i] + x - a[0] )% x == 0)
                    {
                        p = false;
                        break;
                    }
                }

                if (p==true) res[g]=1;
                else res[g]=0;
            }

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

        }
    }
}