using System;
class Program
{
    static void Main()
    {
        int M;
        int N;
        int T = int.Parse(Console.ReadLine());
        int counter;
        int[] ans = new int[T];
        for (int i = 0; i < T; i++)
        {
            counter = 0;
            var s = Console.ReadLine().Split();
            N = int.Parse(s[0]);
            M = int.Parse(s[1]);
            var sn = Console.ReadLine().Split();
            long[] A = new long[N];
            for (int j = 0; j < N; j++)
                A[j] = long.Parse(sn[j]);
            var sm = Console.ReadLine().Split();
            long[] B = new long[M];
            for (int j = 0; j < M; j++)
                B[j] = long.Parse(sm[j]);
            for (int q = 0; q < N; q++)
            {
                for (int w = 0; w < M; w++)
                    if (A[q] == B[w])
                        counter++;
            }
            if (counter != 0)
            {
                ans[i] = 0;
                break;
            }
            else
            {
                Array.Sort(A);
                Array.Sort(B);
                long x = A[N - 1] - A[N - 2];
                for (int j = N - 1; j > 0; j--)
                {
                    if (A[j] - x != A[j - 1])
                    {
                        long y = A[j] - A[j - 1];
                        if (x == 2 * y)
                        {
                            for (int w = M - 1; w >= 0; w--)
                            {
                                if (A[N - 1] - y == B[w])
                                {
                                    counter++;
                                    ans[i] = 0;
                                    break;
                                }
                                else if (A[N - 1] + y == B[w])
                                {
                                    counter++;
                                    ans[i] = 0;
                                    break;
                                }
                            }
                        }
                        else if (y == 2 * x)
                        {
                            for (int w = M - 1; w >= 0; w++)
                            {
                                if (A[N - 1] - x == B[w])

                                {
                                    counter++;
                                    ans[i] = 0;
                                    break;
                                }
                                else if (A[N - 1] + x == B[w])
                                {
                                    counter++;
                                    ans[i] = 0;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int w = M - 1; w >= 0; w--)
                        {
                            if (A[N - 1] - x == B[w])
                            {
                                counter++;
                                ans[i] = 0;
                                break;
                            }
                            else if (A[N - 1] + x == B[w])
                            {
                                counter++;
                                ans[i] = 0;
                                break;
                            }
                        }
                    }
                }
                if (counter != 0)
                    ans[i] = 0;
                else
                    ans[i] = 1;
            }
        }
        for (int z = 0; z < T; z++)
            Console.WriteLine(ans[z]);
    }
}