using System;

namespace ConsoleApp41
{
    class Program
    {
        static void ulaznm(out long n, out long m)
        {
            string[] r = Console.ReadLine().Split();
            n = long.Parse(r[0]);
            m = long.Parse(r[1]);
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            long x;
            int e = 0;
            long[] f = new long[t];
            long[] d = new long[7500];
            long n, m;
            for (int i = 0; i < t; i++)
            {
                ulaznm(out n,out  m);
                string[] s = Console.ReadLine().Split();
                long[] a = new long[n];
                for (long j = 0; j < n; j++)
                {
                    a[j] = long.Parse(s[j]);
                }
                string[] p = Console.ReadLine().Split();
                long[] b = new long[m];
                for (long k = 0; k < m; k++)
                {
                    b[k] = long.Parse(p[k]);
                }
                Array.Sort(a);
                x = a[1] - a[0];
                for (long k = 0; k < m; k++)
                {
                    for (long j = 0; j < n; j++)
                    {
                        for (long g = a[0]; g <= a[n-1]; g += x)
                        {
                            if (a[j] == g && a[j] != b[k])
                            {
                                e++;
                            }
                        }
                    }
                }
                f[i] = e/m;
                d[i] = n;
                e = 0;
            }
            for (int i = 0; i < t; i++)
            {
                if (f[i] == d[i])
                {
                    Console.WriteLine("1");
                }
                else
                    Console.WriteLine("0");
            }
        }
    }
}