using System;
using System.Collections.Immutable;

namespace Zatak3
{
    class Zadatak3
    {
        static void Main(string[] args)
        {
            long T = Convert.ToInt64(Console.ReadLine());
            long[][] A = new long[T][];
            long[][] B = new long[T][];
            for (int i = 0; i < T; i++)
            {
                long inputN = Convert.ToInt64(Console.ReadLine());
                long inputM = Convert.ToInt64(Console.ReadLine());
                A[i] = new long[inputN];
                B[i] = new long[inputM];
                for (int j = 0; j < inputN; j++)
                {
                    A[i][j] = Convert.ToInt64(Console.ReadLine());
                }
                Array.Sort(A[i]);
                for (int j = 0; j < inputM; j++)
                {
                    B[i][j] = Convert.ToInt64(Console.ReadLine());
                }
                Array.Sort(B[i]);
            }
            for (int i = 0; i < T; i++)
            {
                bool temp = false;
                long x = A[i][1] - A[i][0];
                for (int  j = 0;  j < A[i].Length; j++)
                {
                    if (temp == false)
                    {
                        for (int k = 0; k < B[i].Length; k++)
                        {
                            if (A[i][j] == B[i][k] || A[i][j] + x == B[i][k])
                            {
                                Console.WriteLine("0");
                                temp = true;
                                break;
                            }
                        }
                    }
                }
                if (temp == false)
                Console.WriteLine("1");
            }
            
            

        }
    }
}