using System;

namespace Zadatak3
{
    class Program
    {
        static long nzd(long a, long b)
        {
            if (a == 0) return b;
            if (b == 0) return a;
            if (a == b) return a;
            if (a > b) return nzd(a - b, b);
            return nzd(a, b - a);
        }
        static void Main(string[] args)
        {
            int[] izlaz = new int[6];
            int t = Convert.ToInt16(Console.ReadLine());
            for (int i = 1; i < t + 1; i++)
            {
                string[] ulaz = Console.ReadLine().Split(' ');
                int n = Convert.ToInt32(ulaz[0]);
                int m = Convert.ToInt32(ulaz[1]);

                long[] a = new long[n + 1];
                long[] razlike = new long[n + 1];

                long[] b = new long[m + 1];

                ulaz = Console.ReadLine().Split(' ');
                for (int j = 1; j < n + 1; j++) a[j] = Convert.ToInt64(ulaz[j - 1]);
                ulaz = Console.ReadLine().Split(' ');
                for (int j = 1; j < m + 1; j++) b[j] = Convert.ToInt64(ulaz[j - 1]);

                for(int j = 1; j < n; j++) razlike[j] = Math.Abs(a[j] - a[j + 1]);
                razlike[n] = Math.Abs(a[n] - a[1]);

                long nzdIzlaz = razlike[1];
                for (int j = 2; j < n + 1; j++) nzdIzlaz = nzd(nzdIzlaz, razlike[j]);

                long modIzlaz = a[1] % nzdIzlaz;
                int uspesno = 1;

                for (int j = 1; j < m + 1; j++)
                {
                    if (b[j] % nzdIzlaz == modIzlaz)
                    {
                        uspesno = 0;
                        break;
                    }
                }
                izlaz[i] = uspesno;
            }
            for (int i = 1; i < t + 1; i++) Console.WriteLine(izlaz[i]);
            Console.ReadLine();

        }
    }
}