using System;

namespace ConsoleApp5
{
    class Program
    {
        static long minimum(long[] a)
        {
            long min = long.MaxValue;
            for (int i = 0; i < a.GetLength(0); i++)
            {
                if (a[i] < min)
                    min = a[i];
            }
            return min;
        }
        static long nzd(long a, long b)
        {
            while (a != 0 && b != 0)
            {
                if (a > b)
                    a = a % b;
                else
                    b = b % a;
            }
            if (a == 0)
                return b;
            else
                return a;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] tacnost = new int[t];
            int t1 = t - 1;
            while (t != 0)
            {
                string[] s = Console.ReadLine().Split();
                int n = int.Parse(s[0]);
                int m = int.Parse(s[1]);

                long[] a = new long[n];
                string[] r = Console.ReadLine().Split();
                for (int i = 0; i < n; i++)
                {  
                    a[i] = long.Parse(r[i]);
                }

                long mina = minimum(a);
                long tekuci = -1;
                for (int i = 0; i < n; i++)
                {
                    if (a[i] - mina == 0)
                    {
                        continue;
                    }
                    if (tekuci == -1)
                    {
                        tekuci = a[i] - mina;        //prva razlika u nizu
                    }
                    else
                    {
                        tekuci = nzd(tekuci, a[i] - mina);
                    }
                }

                long[] b = new long[m];

                string[] q = Console.ReadLine().Split();
                for (int i = 0; i < m; i++)
                {

                    b[i] = long.Parse(q[i]);
                }

                int ok = 1;
                for (int i = 0; i < m; i++)
                {
                    if (ok == 1)
                    {
                        long razlika = Math.Abs(b[i] - mina);
                        if (razlika % tekuci == 0)
                            ok = 0;
                    }
                }
                tacnost[t - 1] = ok;
                t--;
            }
            for (int i = t1; i >= 0; i--)
            {
                Console.WriteLine(tacnost[i]);
            }
        }
    }
}