using System;

namespace Zalbe
{
    class Program
    {
        public static void sort(long[] input, int n)
        {
            var itemMoved = false;
            do
            {
                itemMoved = false;
                for (int i = 0; i < n - 1; i++)
                {
                    if (input[i] > input[i + 1])
                    {
                        var lowerValue = input[i + 1];
                        input[i + 1] = input[i];
                        input[i] = lowerValue;
                        itemMoved = true;
                    }
                }
            } while (itemMoved);
        }
        static long findGCD(long[] arr, int n)
        {
            long result = arr[0];
            for (int i = 1; i < n; i++)
            {
                result = gcd(arr[i], result);

                if (result == 1)
                {
                    return 1;
                }
            }

            return result;
        }
        static long gcd(long a, long b)
        {
            if (a == 0)
                return b;
            return gcd(b % a, a);
        }
        static void Main(string[] args)
        {
            int t, n, m;
            t=int.Parse(Console.ReadLine());
            int[] ans= new int[t];
            for (int j = 0; j < t; ++j)
            {
                ans[j] = 1;
                string[] x=Console.ReadLine().Split();
                n = int.Parse(x[0]);
                m = int.Parse(x[1]);
                long[] a=new long[n];
                long[] e = new long[n-1];
                long[] b=new long[m];
                x = Console.ReadLine().Split();
                for (int i = 0; i < n; ++i)
                {
                    a[i]=long.Parse(x[i]);
                }
                x = Console.ReadLine().Split();
                for (int i = 0; i < m; ++i)
                {
                    b[i] = long.Parse(x[i]);
                }
                sort(a, n);
                sort(b, m);
                int w= 0;
                while(w<n-1)
                {
                    e[w] = a[w + 1] - a[w];
                    w++;
                }
                long nzd = findGCD(e, n-1);
                long q=a[0];
                w = 0;
                while(q>b[0])
                {
                    while(w<m)
                    {
                        if(q==b[w])
                        {
                            ans[j] = 0;
                        }
                        w++;
                    }
                    w = 0;
                    q -= nzd;
                }
                q = a[0];
                w = 0;
                while (q < b[m-1])
                {
                    while (w < m)
                    {
                        if (q == b[w])
                        {
                            ans[j] = 0;
                        }
                        w++;
                    }
                    w = 0;
                    q += nzd;
                }
            }
            for (int i = 0; i < t; ++i)
            {
                Console.WriteLine(ans[i]);
            }
        }
    }
}