using System;

namespace Takmicenje_3
{
    class Program
    {
        static void mergeSort(long[] a, int l, int d)
        {
            if (l < d)
            {
                int s = (l + d) / 2;
                mergeSort(a, l, s);
                mergeSort(a, s + 1, d);
                merge(l, s, d, a);
            }
        }
        static void merge(int l, int s, int d, long[] a)
        {
            long[] b = new long[d - l + 1];
            int k = 0;
            int i = l; int j = s + 1;
            while (i <= s && j <= d)
                if (a[i] < a[j]) b[k++] = a[i++];
                else b[k++] = a[j++];
            while (i <= s) b[k++] = a[i++];
            for (i = 0; i < k; i++)
                a[l + i] = b[i];
        }

        static bool checkStep(long[] a,long[] b,long step,int A,int B)
        {
            int k = 0;
            int i = 1;
            bool p = false;
            if(a[A-1]>b[B-1])
                while(a[0]+k * step <= a[A - 1])
                {
                    if (a[0] + k * step == a[i]) i++;
                    for (int j = 0; j < B; j++)
                        if (a[0] + k * step == b[j]) return false;
                    if (i == A) p = true;
                    k++;
                }
            else
                while (a[0] + k * step <= b[B - 1])
                {
                    if (a[0] + k * step == a[i]) i++;
                    for (int j = 0; j < B; j++)
                        if (a[0] + k * step == b[j]) return false;
                    if (i == A) p = true;
                    k++;
                }
            return p;
        }

        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            long step = 0;
            for(int i = 0; i < T; i++)
            {
                string[] s = Console.ReadLine().Split();
                int A = int.Parse(s[0]);
                int B = int.Parse(s[1]);
                long[] a = new long[A];
                long[] b = new long[B];
                s = Console.ReadLine().Split();
                for (int j = 0; j < A; j++) a[j] = long.Parse(s[j]);
                mergeSort(a, 0, A - 1);
                s = Console.ReadLine().Split();
                for (int j = 0; j < B; j++) b[j] = long.Parse(s[j]);
                mergeSort(b, 0, B - 1);
                int k = A - 1;
                while (k > 0)
                {
                    if (step == 0) step = a[k] - a[k - 1];
                    else if (a[k] - a[k - 1] < step) step = a[k] - a[k - 1];
                    k--;
                }
                if (checkStep(a, b, step, A, B)) Console.WriteLine("1");
                else Console.WriteLine("0");
                step = 0;
            }
        }
    }
}