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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            string resenje ="";
            for (int i = 0; i < T; i++)
            {
                string[] x = Console.ReadLine().Split(' ');
                int N = int.Parse(x[0]);
                int M = int.Parse(x[1]);

                x = Console.ReadLine().Split(' ');
                long[] prihvacene = new long[N];
                long min = Min(x);
                
                for (int j = 0; j < N; j++)
                    prihvacene[j] = long.Parse(x[j])-min;

                List<long> moguceX = SviZajednickiDelioci(prihvacene);

                x = Console.ReadLine().Split(' ');
                long[] odbijene = new long[M];

                for (int j = 0; j < M; j++)
                {
                    odbijene[j] = long.Parse(x[j]) - min;

                    for (int k = 0; k < moguceX.Count; k++)
                        if (odbijene[j] % moguceX[k] == 0)
                        {
                            moguceX.RemoveAt(k);
                            k--;
                        }
                }

                resenje+= (moguceX.Count == 0 ? 0 : 1) + "\n";
            }

            Console.Write(resenje);
            Console.ReadKey();
        }

        static long Min(string[] x)
        {
            long min = long.Parse(x[0]);

            for(int i = 0; i < x.Length; i++)
                if(long.Parse(x[i]) < min)
                    min = long.Parse(x[i]);

            return min;
        }

        static List<long> SviZajednickiDelioci(long[] niz)
        {
            List<long> r = new List<long>();
            int l = niz.Length;
            r.Add(1);
            
            int pr = (niz[l-1] != 0) ? l-1 : 0;

            r.AddRange(sviProstiDelioci(niz[pr]));

            foreach (int i in niz)
            {
                for (int j = 1; j < r.Count; j++)
                    if (i % r[j] != 0)
                    {
                        r.RemoveAt(j);
                        j--;
                    }
            }

            return r;
        }

        static List<long> sviProstiDelioci(long n)
        {
            List<long> Delioci = new List<long>();

            while (n % 2 == 0)
            {
                if (!Delioci.Contains(2))
                    Delioci.Add(2);
                n /= 2;
            }

            for (int i = 3; i * i <= n; i += 2)
            {
                while (n % i == 0)
                {
                    if (!Delioci.Contains(i))
                        Delioci.Add(i);
                    n /= i;
                }
            }

            int n1 = (int)n;

            if (n1 > 2 && !Delioci.Contains(n1))
                Delioci.Add(n1);
            return Delioci;
        }
    }
}