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

namespace takmicenje1
{
    class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            int[] resenja = new int[T];
            for (int i = 0; i < T; i++)
            {
                string[] unos = Console.ReadLine().Split();
                int N = int.Parse(unos[0]);
                int M = int.Parse(unos[1]);
                Int64[] prihvaceni = new Int64[N];
                Int64[] odbijeni = new Int64[M];
                string[] unosN = Console.ReadLine().Split();
                for (int j = 0; j < N; j++)
                {
                    prihvaceni[j] = Int64.Parse(unosN[j]);
                }
                string[] unosM = Console.ReadLine().Split();
                for (int j = 0; j < M; j++)
                {
                    odbijeni[j] = Int64.Parse(unosM[j]);
                }
                Array.Sort(prihvaceni);
                Array.Sort(odbijeni);
                Int64[] razlike = new Int64[N - 1];


                bool postojiX = true;
                Int64 min = Int64.MaxValue;
                for (int j = 0; j < N - 1; j++)
                {
                    razlike[j] = prihvaceni[j + 1] - prihvaceni[j];
                    if (razlike[j] < min)
                        min = razlike[j];
                }
                for (int j = 0; j < N - 1; j++)
                {
                    if (razlike[j] % min != 0)
                        postojiX = false;
                }
                razlike = new Int64[M];

                if (postojiX)
                {
                    Int64 a1 = prihvaceni[0] % min;
                    for (int j = 0; j < M; j++)
                    {
                        if ((odbijeni[j] - a1) % min == 0)
                        {
                            postojiX = false;
                        }
                    }
                }

                resenja[i] = (postojiX) ? 1 : 0;
            }
            foreach (int element in resenja)
                Console.WriteLine(element);
        }
    }
}