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

namespace Zalbe
{
    class Tanulo
    {
        public int N;
        public int M;
        public ulong[] A;
        public ulong[] B;
        public ulong legkisebbKulonbseg;
        public int poz;
        public ulong kezdoszam;
        public bool jo = false;
        public int igaz = 0;

        public Tanulo()
        {
            string[] sor = Console.ReadLine().Split();
            N = int.Parse(sor[0]);
            M = int.Parse(sor[1]);
            A = new ulong[N];
            B = new ulong[M];

            sor = Console.ReadLine().Split();
            for (int i = 0; i < N; i++)
            {
                A[i] = ulong.Parse(sor[i]);
            }

            sor = Console.ReadLine().Split();
            for (int i = 0; i < M; i++)
            {
                B[i] = ulong.Parse(sor[i]);
            }
        }

        public void Kulonbseg()
        {
            Array.Sort(A);
            ulong temp;
            ulong y;
            poz = 0;
            y = A[1] - A[0];
            for (int i = 1; i < N-1; i++)
            {
                temp = A[i + 1] - A[i];
                if (temp < y)
                {
                    y = temp;
                    poz = i;
                }                
            }
            legkisebbKulonbseg = y;
        }

        public int OsztokKeresese()
        {
            for (ulong o = 1; o <= legkisebbKulonbseg; o++)
            {
                jo = true;
                if (legkisebbKulonbseg % o == 0)
                {
                    //Kezdoszam szamitasa (pl 1)
                    kezdoszam = A[0] % o;

                    //Elfogadott ev maradeka
                    for (int j = 0; j < N; j++)
                    {
                        if ((A[j] - kezdoszam) % o != 0)
                        {
                            jo = false;
                            break;
                        }
                    }
                    if (jo)
                    {
                        //Elutasitott ev maradeka
                        for (int j = 0; j < M; j++)
                        {
                            if ((B[j] - kezdoszam) % o == 0)
                            {
                                jo = false;
                                break;
                            }
                        }
                    }
                    if (jo)
                    {
                        igaz = 1;
                        //Console.WriteLine(o);
                        break;
                    }
                }
            }
            
            return igaz;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int T = Convert.ToInt32(Console.ReadLine());
            int[] eredmenyek = new int[T];
            for (int i = 0; i < T; i++)
            {
                Tanulo tanulo = new Tanulo();
                tanulo.Kulonbseg();
                eredmenyek[i] = tanulo.OsztokKeresese();
            }
            for (int i = 0; i < T; i++)
            {
                Console.WriteLine(eredmenyek[i]);
            }
           // Console.ReadLine();
        }
    }
}