using System;

namespace Zad3
{
    class Program
    {
        static void Main(string[] args)
        {
            //N-prihvacene zalbe
            //M-odbijene zalbe
            //T-Broj takmicara
            int T,N,M,br1=0;
            long[] x,y;
            int[] a1;
            long z,v,v1,v2;
            string c;
            string[] a;
            T = int.Parse(Console.ReadLine());
            a1 = new int[T];
            for (int i = 0; i < T; i++)
            {
                c = Console.ReadLine();
                a = c.Split();
                N = int.Parse(a[0]);
                M = int.Parse(a[1]);
                x = new long[N];
                y = new long[M];
                c = Console.ReadLine();
                a = c.Split();
                for (int k = 0; k < N; k++)
                    {
                    x[k] = long.Parse(a[k]);
                    }
                      
                    
                c = Console.ReadLine();
                a = c.Split();
                    for (int l = 0; l < M; l++)
                    {
                        y[l] = long.Parse(a[l]);
                    }

                Array.Sort(y);
                Array.Sort(x);

                z = x[1] - x[0];
                v = z;
                int o = 0;
                for (int j = 0; j < x.Length-1; j++)
                {
                    v = x[j + 1] - x[j];
                    if (v == z) o++;
                    else if (v % z == 0)
                    {
                        v1 = x[j];
                        br1 = 0;
                        for (int j1 = 0; j1 < v / z; j1++)  
                        {
                            v1 += z;
                            v2 = Array.Find(y, element => element == v1);
                            if ( v2 == v1) break;
                            else br1++;
                        }
                        if (br1 == v / z) o++;

                    }

                }
                if (o == x.Length-1) a1[i] = 1;
                else a1[i] = 0;
                

            }
            for (int i = 0; i < T; i++)
            {
                Console.WriteLine(a1[i]);
            }
            

        }




        }
    }