using System;
using System.Linq;

namespace Žalbe
{
    class Program
    {
        static void ulaznm(out int n, out int m)
        {
            string[] s = Console.ReadLine().Split();
            n = int.Parse(s[0]);
            m = int.Parse(s[1]);
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split();
            string[] p = Console.ReadLine().Split();
            int n, m;
            int br = 0;
            long x = 0;
            for (int i = 0; i <= t; i++)
            {
                ulaznm(out n, out m);
                long[] a = new long[n];
                long[] b = new long[m];
                for (int j = 0; j < n; j++)
                {
                    a[j] = long.Parse(s[j]);
                }
                Array.Sort(a);
                x = Math.Abs(a[1] - a[0]);
                long max = a.Max();
                for (int k = 0; k < n; k++)
                {
                    b[k] = long.Parse(p[k]);
                }
                for (int q = 2; q < n; q++)
                {
                    for (long w = 1; w <= max; w++)
                    {
                        for (int r = 0; r < m; r++)
                        {
                            if (x * w + a[q - 1] == a[q] && a[q] != b[r]) 
                            {
                                br++;
                            }
                        }
                    }
                }
                if (br == n)
                {
                    Console.WriteLine("1");
                }
                else
                {
                    Console.WriteLine("0");
                }
            }
        }
    }
}