using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp27
{
    class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            int[] tacnost= new int[T]; 
            for(int i=0;i<T;i++)
            {
                string[] s = Console.ReadLine().Split();
                int N = int.Parse(s[0]);
                int M = int.Parse(s[1]);
                long[] A = new long[N];
                long[] B = new long[M];
                string[] s1= Console.ReadLine().Split();
                string[] s2 = Console.ReadLine().Split();
                for (int j = 0; j < N; j++)
                    A[j] = Convert.ToInt64(s1[j]);
                for(int k = 0; k < M; k++)
                    B[k] = Convert.ToInt64(s2[k]);
                bool promena;
                do
                {
                    promena = false;
                    for (int q = 0; q < N - 1; q++)
                    {
                        if (A[q] > A[q + 1])
                        {
                            long p = A[q];
                            A[q] = A[q + 1];
                            A[q + 1] = p;
                           promena = true;
                        }
                    }
                } while (promena);
                do
                {
                    promena = false;
                    for (int z = 0; z < M - 1; z++)
                    {
                        if (B[z] > B[z + 1])
                        {
                            long p = B[z];
                           B[z] = B[z + 1];
                            B[z + 1] = p;
                            promena = true;
                        }
                    }
                } while (promena);
                if (N==2||A[1]-A[0]==A[2]-A[1])
                {
                    tacnost[i] = 1;
                }
                else
                    tacnost[i] = 0;
            }
            for (int i = 0; i < T; i++)
                Console.WriteLine(tacnost[i]);

            
        }
    }
}