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;
            long y;
            int e = 0;
            long[] f = new long[t];
            long[] d = new long[7500];
            long n, m;
            for (int i = 0; i < t; i++)
            {
                e = 0;
                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);
                Array.Sort(b);
                x = a[1] - a[0];
                for (int 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++;
                            }
                            else if (a[j] == b[k])
                            {
                                e = 0;
                            }
                        }
                    }
                }
                for (int k = 0; k < m; k++)
                {
                    for (long g = b[0]; g <= b[m - 1]; g += x)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            if(a[j]==g)
                            {
                                e = 0;
                            }
                        }
                    }
                }
                for (int k = 1; k < m; k++)
                {
                    if (b[k] - b[k - 1] == x)
                    {
                        e = 0;
                    }
                }
                f[i] = e/m;
                d[i] = n;
            }
            for (int i = 0; i < t; i++)
            {
                if(f[i]==0) Console.WriteLine("0");
                else if (f[i] == d[i])
                {
                    Console.WriteLine("1");
                }
            }
        }
    }
}