using System;
using System.Collections.Generic;

namespace ZALBE
{
    class Program3
    {
        static List<long> delioci(long max, long sqrtmax)
        {
            List<long> del = new List<long>();
            if (max % 2 == 0)
            {
                del.Add(2);
                if (2 % (max / 2) != 0)
                    del.Add(max / 2);
            }
            for (long i = 3; i <= sqrtmax; i+=2)
                if(max % i == 0)
                {
                    del.Add(i);
                    if (i % (max / i) != 0 && del.FindIndex(x => x == max/i) == -1)
                        del.Add(max / i);
                }
            del.Add(max);
            return del;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            for (int j = 0; j < t; j++)
            {
                string[] unos = Console.ReadLine().Split();
                int n = int.Parse(unos[0]);
                int m = int.Parse(unos[1]);

                long[] p = new long[n];
                long[] o = new long[m];

                unos = Console.ReadLine().Split();
                p[0] = long.Parse(unos[0]);
                p[1] = long.Parse(unos[1]);
                long max = (p[0] - p[1] > 0) ? p[0] - p[1] : p[1] - p[0];
                for (int i = 2; i < n; i++)
                {
                    p[i] = long.Parse(unos[i]);
                    long max1 = (p[i] - p[i - 1] > 0) ? p[i] - p[i - 1] : p[i - 1] - p[i];
                    if (max1 < max)
                        max = max1;
                }

                bool triger = true;
                unos = Console.ReadLine().Split();
                for (int i = 0; i < m; i++)
                    o[i] = long.Parse(unos[i]);

                if (max == 1)
                {
                    Console.WriteLine("0");
                    continue;
                }

                long sqrtmax = (long)(Math.Sqrt(max));
                long[] deli = delioci(max, sqrtmax).ToArray();
                int l = deli.Length;

                for(int i = 0; i < l && triger; i++)
                {
                    bool ok = true;
                    max = (p[0] - p[1] > 0) ? p[0] - p[1] : p[1] - p[0];
                    long provera = max % deli[i];
                    for (int k = 2; k < n; k++)
                    {
                        long max1 = (p[k] - p[k - 1] > 0) ? p[k] - p[k - 1] : p[k - 1] - p[k];
                        if (max1 % deli[i] != provera)
                        {
                            ok = false;
                            break;
                        }
                    }
                    if(ok)
                        {
                            long c = p[0] % deli[i];
                            for (int k = 0; k < m && ok; k++)
                                if (o[k] % deli[i] == c)
                                    ok = false;
                            if (ok)
                            {
                                Console.WriteLine("1");
                                triger = false;
                            }
                        }
                }

                if (triger)
                    Console.WriteLine("0");
            }
        }
    }
}