using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
    static void Main(string[] args)
    {
        int T = int.Parse(Console.ReadLine());
        int[] resenja = new int[T];
        for (int i = 0; i < T; i++)
        {
            bool ret = true;
            int N, M;
            string[] inp = Console.ReadLine().Split(' ');
            N = int.Parse(inp[0]);
            M = int.Parse(inp[1]);
            long[] A = new long[N];
            long[] B = new long[M];
            string[] inp2 = Console.ReadLine().Split(' ');
            for (int j = 0; j < N; j++)
            {
                A[j] = long.Parse(inp2[j]);
            }
            string[] inp3 = Console.ReadLine().Split(' ');
            for (int j = 0; j < M; j++)
            {
                B[j] = long.Parse(inp3[j]);
            }
            long nmin = Math.Abs(A[0] - A[1]);
            int amin;
            if (A[0] > A[1]) amin = 1;
            else amin = 0;
            for(int j = 1; j < N-1 && ret != false; j++)
            {
                if (A[j+1] < amin) amin = j+1;
                long nf = Math.Abs(A[j] - A[j + 1]);
                long at = nf;
                long bt = nmin;
                while (at != 0 && bt != 0)
                {
                    if (at > bt)
                    {
                        at = at % bt;
                    }
                    else if (at < bt)
                    {
                        bt = bt % at;
                    }
                }
                if (at > 0)
                {
                    nmin = at;
                }
                else if (bt > 0)
                {
                    nmin = bt;
                }
            }
            for (int j = 0; j < M && ret != false; j++)
            {
                if ((Math.Abs(B[j] - A[amin]) % nmin) == 0) ret = false;
            }
            if (ret) resenja[i] = 1;
            else resenja[i] = 0;
        }
        for(int i = 0; i < T; i++)
        {
            Console.WriteLine(resenja[i]);
        }
    }
}