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

namespace ConsoleApp1
{
    class Program
    {
        public static ulong nzd(ulong a, ulong b)
        {
            if (a < b)
            {
                a = a + b;
                b = a - b;
                a = a - b;
            }
            while (a > 0 && b > 0)
            {
                a = a % b;
                a = a + b;
                b = a - b;
                a = a - b;
            }
            return a;
        }
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());
            ulong K = 1;
            ulong A0, A1, A2, A3, B;
            int f = 1;
            ulong P, P1;
            for (int i = 0; i < t; i++)
            {
                //Console.WriteLine("Koliko dobrih godina ima");
                int N = Convert.ToInt32(Console.ReadLine());
                //Console.WriteLine("Koliko osih godina ima");
                int M = Convert.ToInt32(Console.ReadLine());
                //Console.WriteLine("Sve dobre");
                A0 = Convert.ToUInt64(Console.ReadLine());
                A2 = Convert.ToUInt64(Console.ReadLine());
                if (A0 > A2) P1 = A0 - A2;
                else P1 = A2 - A0;
                if (N == 2) K = P1;
                for (int j = 2; j < N; j++)
                {
                    A1 = A2;
                    A2 = Convert.ToUInt64(Console.ReadLine());
                    if (A1 > A2) P = A1 - A2;
                    else P = A2 - A1;
                    K = nzd(P, P1);
                    P1 = P;
                }
                //Console.WriteLine("Sve lose");
                for (int j = 0; j < M; j++)
                {
                    B = Convert.ToUInt64(Console.ReadLine());
                    if (B % K == A0 % K) f = 0;
                }
                if (K == 1) Console.WriteLine("0");
                else Console.WriteLine(f);
            }
        }
    }
}