using System;
using System.Collections;
using System.Collections.Generic;

class Program
    {
    static void Main(string[] args)
    {
        int T;
        T = Convert.ToInt32(Console.ReadLine());
        if (T < 1 || T > 5)
            Console.WriteLine("T < 1 ili T > 5");
        TClass[] t = new TClass[T];
        for (int k = 0; k < T; k++)
        {
            TClass D = new TClass();
            t[k] = D;
            string[] S1 = Console.ReadLine().Split();
            if (Convert.ToInt32(S1[0]) < 2 || Convert.ToInt32(S1[0]) > 75000 || Convert.ToInt32(S1[1]) < 2 || Convert.ToInt32(S1[1]) > 75000)
                Console.WriteLine("N ili M je < 2, ili > 75000");
            t[k].N = Convert.ToInt32(S1[0]);
            t[k].M = Convert.ToInt32(S1[1]);

            t[k].A = new long[t[k].N];
            t[k].B = new long[t[k].M];
            string[] S2 = Console.ReadLine().Split();
            for (int i = 0; i < t[k].N; i++)
                t[k].A[i] = Convert.ToInt64(S2[i]);
            string[] S3 = Console.ReadLine().Split();
            for (int j = 0; j < t[k].M; j++)
                t[k].B[j] = Convert.ToInt64(S3[j]);
        }

        for(int k = 0; k < T; k++)
        {
            Array.Sort(t[k].A);
            long l1 = t[k].A[1] - t[k].A[0];
            for(int i = 0; i < t[k].N-1; i++)
            {
                if ((t[k].A[i + 1] - t[k].A[i]) % l1 != 0)
                    t[k].X = 0;
            }
            if (t[k].X == 1)
            {
                Array.Sort(t[k].B);
                for (int i = 0; i < t[k].M - 1; i++)
                {
                    if ((t[k].B[i + 1] - t[k].B[i]) % l1 != 0)
                    {
                        t[k].X = 1;
                        break;
                    }
                    if (i == t[k].M - 2 && t[k].B[i + 1] - t[k].B[i] == l1)
                    {
                        t[k].X = 0;
                    }
                }
            }

            Console.WriteLine(t[k].X);
        }
    }
}

class TClass
{
    public int N = 1, M = 1;
    public int X = 1;
    public long[] A, B;
}