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

namespace Takmicenje_3
{
    class Program
    {
        public static void sortiranje(int[] a, int n)
        {
            int i, j;
            for (i = 0; i < n - 1; i++)
                for (j = i + 1; j < n; j++)
                    if (a[i] > a[j])
                        razmeni(ref a[i], ref a[j]);
        }
        public static void razmeni(ref int x, ref int y)
        {
            int p = x;
            x = y;
            y = p;
        }

        static void Main(string[] args)
        {
            int takmicari, i, j, k, l, x, y, minX;
            bool moze;
            string izlaz = "";
            takmicari = int.Parse(Console.ReadLine());
            for (i = 0; i < takmicari; i++)
            {
                string[] niz = Console.ReadLine().Split();
                int brPrihvacenihG = Int32.Parse(niz[0]);
                int brNePrihvacenihG = Int32.Parse(niz[1]);
                int[] prihvaceneG = new int[brPrihvacenihG];
                int[] neprihvaceneG = new int[brNePrihvacenihG];
                string[] prihvaceneglasine = Console.ReadLine().Split();
                for (j = 0; j < brPrihvacenihG; j++)
                {
                    prihvaceneG[j] = Int32.Parse(prihvaceneglasine[j]);
                }
                string[] neprihavceneglasine = Console.ReadLine().Split();
                for (k = 0; k < brNePrihvacenihG; k++)
                {
                    neprihvaceneG[k] = Int32.Parse(neprihavceneglasine[k]);
                }
                sortiranje(prihvaceneG, brPrihvacenihG);
                sortiranje(neprihvaceneG, brNePrihvacenihG);
                minX = prihvaceneG[1] - prihvaceneG[0];
                for (j = 1; j < brPrihvacenihG; j++)
                {
                    x = prihvaceneG[j] - prihvaceneG[j - 1];
                    if (x < minX)
                        minX = x;
                }
                moze = true;
                for (j = 1; j < brPrihvacenihG; j++)
                {
                    if ((prihvaceneG[j] - prihvaceneG[1]) % minX != 0)
                        moze = false;
                }
                for (j = 0; j < brNePrihvacenihG; j++)
                {
                    if ((neprihvaceneG[j] - prihvaceneG[1]) % minX == 0)
                        moze = false;
                }
                if (moze == true)
                    izlaz += "1\n";
                else
                    izlaz += "0\n";
            }
            Console.WriteLine("{0}", izlaz);
            Console.ReadLine();
        }
    }
}