using System;
using System.Collections.Generic;
namespace ConsoleApp59
{
    class Program
    {
        static void Main(string[] args)
        {
            bool retard;
            int n = int.Parse(Console.ReadLine());
            long dodo = 0;
            for (int i = 0; i < n; i++)
            {
                Dictionary<long, long> gingi = new Dictionary<long, long>();
                retard = true;
                string[] s = Console.ReadLine().Split();
                int a = int.Parse(s[0]);
                int b = int.Parse(s[1]);
                string[] x = Console.ReadLine().Split();
                string[] z = Console.ReadLine().Split();
                for (int j = 0;j<z.Length;j++) {
                    gingi.Add(long.Parse(z[j]), 0);
                    
                }
                long[] niz = new long[a];

                for (int j = 0; j < a-1; j++) {
                    niz[j] =Math.Abs( long.Parse(x[j]) - long.Parse(x[j+1]));
                }
                dodo = nzd(niz[0], niz[1]);
                for (int j = 0; j<niz.Length;j++) {
                    dodo = nzd(dodo, niz[j]);
                }
                for (int j = 0; j < z.Length;j++) {
                    if (Math.Abs(long.Parse(z[j]) - long.Parse(x[0])) % dodo == 0)
                    {
                        retard = false;
                    }
                    else
                        retard = true;
                
                }
                if (retard) {
                    Console.WriteLine(1);
                }
                if (!retard)
                {
                    Console.WriteLine(0);
                }

            }
        }

        private static long nzd(long a, long b)
        {
            while (a != 0 && b != 0)
            {
                if (a > b)
                    a %= b;
                else
                    b %= a;
            }

            return a | b;
        }
    }
}