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

namespace Zalbe
{
    class Program
    {
        static void Main(string[] args)
        {
            int brojTakmicara = Int32.Parse(Console.ReadLine());
            for (int i = 0; i < brojTakmicara; i++)
            {
                String[] line = Console.ReadLine().Split();
                int brojTacnih = Int32.Parse(line[0]);
                int brojNetacnih = Int32.Parse(line[1]);

                Boolean tacno = true;
                long[] tacni = new long[brojTacnih];
                long[] neTacni = new long[brojNetacnih];

                line = Console.ReadLine().Split();

                for (int j = 0; j < tacni.Length; j++)
                {
                    tacni[j] = long.Parse(line[j]);
                }

                line = Console.ReadLine().Split();
                for (int j = 0; j < neTacni.Length; j++)
                {
                    neTacni[j] = long.Parse(line[j]);
                }


                long x = long.MaxValue;
                Array.Sort(tacni);
                for (int j = 0; j < tacni.Length - 1; j++)
                {
                    if (tacni[j] - tacni[j + 1] < x)
                    {
                        x = tacni[j] - tacni[j + 1];
                    }
                }

                for (int j = 0; j < tacni.Length - 1; j++)
                {
                    if ((tacni[j] - tacni[j + 1]) % x == 0)
                    {
                        continue;
                    }
                    tacno = true;
                    break;
                }

                for (int j = 0; j < neTacni.Length; j++)
                {
                    if (Math.Abs(tacni[0] - neTacni[j]) % x == 0)
                    {
                        tacno = false;
                        break;
                    }

                }
                Console.WriteLine((tacno) ? 1 : 0);
                
            }
        }
    }
}