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

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