using System;

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 ulazn(out long a, int n)
        {
            a = 1;
            string[] s = Console.ReadLine().Split();
            for (int i = 0; i < n; i++)
            {
                a = long.Parse(s[i]);
            }
            Array.Sort(s);
        }

        static void ulazm(out long b, int m)
        {
            b = 1;
            string[] s = Console.ReadLine().Split();
            for (int i = 0; i < m; i++)
            {
                b = long.Parse(s[i]);
            }
            Array.Sort(s);
        }

        static long aprog(long x, long y)
        {
            long e = Math.Abs(x - y);
            return 1;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int n, m;
            long a, b;
            int br1 = 0;
            int br2 = 0;
            for (int i = 1; i <= t; i++)
            {
                ulaznm(out n, out m);
                ulazn(out a, n);
                ulazm(out b, m);
                for (int j = 0; j < n; j++)
                {
                    if(aprog(a,a)==1)
                    {
                        br1++;
                    }
                }
                for (int j = 0; j < n; j++)
                {
                    if (aprog(b, b) == 1)
                    {
                        br2++;
                    }
                }
                if (br1 == n && br2 == m)
                {
                    Console.WriteLine("1");
                }
                else
                    Console.WriteLine("0");
                br1 = 0;
                br2 = 0;
            }
            
        }
    }
}