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

namespace TakProg3
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            string[] wl = new string[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]);
                ulong[] tzp = new ulong[n];
                ulong[] tzo = new ulong[m];
                s = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                    tzp[j] = ulong.Parse(s[j]);
                Array.Sort(tzp);
                s = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                    tzo[j] = ulong.Parse(s[j]);
                Array.Sort(tzo);
                ulong pk = (tzp[n - 1] - tzp[0]) / (ulong)n;
                ulong ok = (tzo[m - 1] - tzo[0]) / (ulong)m;
                if (pk == ok)
                    wl[i] = "0";
                else
                    wl[i] = "1";
            }
            for (int i = 0; i < t; i++)
                Console.WriteLine(wl[i]);
        }
    }
}