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

namespace ConsoleApplication3
{
    class Program
    {

        static void quicksort(long[] A, int levi, int desni)
        {
            int x;
            if(levi < desni)
            {
                x = podela(A, levi, desni);
                quicksort(A, levi, x - 1);
                quicksort(A, x + 1, desni);
            }
        }

        static int podela(long[] A, int levi, int desni)
        {
            long pivot = A[desni];
            int i = levi;
            for (int j = levi; j < desni; j++)
            if(A[j]<=pivot)
            {
                long t = A[j];
                A[j] = A[i];
                A[i] = t;
                i++;
            }
            long x = A[i];
            A[i] = A[desni];
            A[desni] = x;
            return i;
        }

        static void Main(string[] args)
        {
            int T = Convert.ToInt16(Console.ReadLine());
            int N, M;
            long X, B;
            string[] s;
            long[] A;
            int R=0;
            for(int i = 0; i<T;i++)
            {
                s = Console.ReadLine().Split(' ');
                N = Convert.ToInt32(s[0]);
                M = Convert.ToInt32(s[1]);
                A = new long[N];
                s = Console.ReadLine().Split(' ');
                for (int br1 = 0; br1 < N; br1++)
                    A[br1] = Convert.ToInt64(s[br1]);
                quicksort(A, 0, N - 1);
                X = A[1] - A[0];
                for (int br3 = 1; br3 < N - 1; br3++)
                    if ((A[br3] + X) > A[br3 + 1]) X = A[br3 + 1] - A[br3];
                s = Console.ReadLine().Split(' ');
                for (int br4 = 0; br4 < M; br4++)
                {
                    B = Convert.ToInt64(s[br4]);
                    if ((A[0] - B) % X == 0)
                    {
                        R = 0;
                        break;
                    }
                    else R = 1;
                }
                Console.WriteLine(R);
            }
        }
    }
}