using System;

namespace ZALBE
{
    class Program3
    {
        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;
                if (max == 1)
                {
                    Console.WriteLine("0");
                    triger = false;
                }

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

                for(long k = 2; k <= Math.Sqrt(max) && triger; k++)
                    if(max % k == 0)
                    {
                        bool ok = true;
                        long c = p[0] % k;
                        for (int i = 0; i < m && ok; i++)
                            if (o[i] % k == c)
                                ok = false;
                        if(ok)
                        {
                            Console.WriteLine("1");
                            triger = false;
                        }
                    }

                if (triger)
                {
                    bool ok = true;
                    for (int i = 0; i < m && ok; i++)
                        if (p[0] % max == o[i] % max)
                            ok = false;
                    if(ok)
                    {
                        Console.WriteLine("1");
                        triger = false;
                    }
                }
                if(triger)
                    Console.WriteLine("0");
            }
        }
    }
}