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

namespace ConsoleApplication154
{
    class Program
    {
        static int NZD(int a, int b)
        {
            while(b!=0)
            {
                int p = b;
                b = a % b;
                a = p;
            }
            return a;
        }
        static int ostatak(int n, int []a)
        {
            int x = Math.Abs(a[1]-a[0]);
            for(int i=1;i<n;i++)
            {
                x = NZD(x, Math.Abs(a[i]-a[i-1]));
            }
            return x;
        }
        static void Main(string[] args)
        {
            
            int n = int.Parse(Console.ReadLine());
            bool[] m = new bool[n];
            for(int i=0;i<n;i++)
            {
                string s = Console.ReadLine();
                string[] r = s.Split(' ');
                
                int N = int.Parse(r[0]);
                int[] a = new int[N];
                int M = int.Parse(r[1]);
                int[] b = new int[M];
                string s1 = Console.ReadLine();
                string[] r1 = s1.Split(' ');
                string s2 = Console.ReadLine();
                string[] r2 = s2.Split(' ');
                m[i] = true;
                for(int j=0;j<N;j++)
                {
                    a[j] = int.Parse(r1[j]);
                }
                for (int k = 0; k < M; k++)
                {
                    b[k] = int.Parse(r2[k]);
                }
                for(int l=0;l<M;l++)
                {
                    int S=ostatak(N,a);
                    if((b[l]-a[0])%S==0)
                    {
                        m[i] = false;
                        break;
                    }
                    
                }
            }
            for(int c=0;c<n;c++)
            {
                if(m[c])
                {
                    Console.WriteLine(1);
                   
                }
                else
                {
                    Console.WriteLine(0);
                }
            }
            Console.ReadLine();
        }
    }
}