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

namespace Zalbe
{
    class Program
    {
        static void Main(string[] args)
        {
            byte t = byte.Parse(Console.ReadLine());

            long[][] a = new long[t][];
            long[][] b = new long[t][];

            bool[] resenja = new bool[t];

            for (byte i = 0; i < t; i++)
            {
                string[] str = Console.ReadLine().Split();
                int n = int.Parse(str[0]);
                int m = int.Parse(str[1]);

                a[i] = new long[n];
                b[i] = new long[m];

                str = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                {
                    a[i][j] = long.Parse(str[j]);
                }
                Array.Sort(a[i]);

                str = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    b[i][j] = long.Parse(str[j]);
                }
                Array.Sort(b[i]);

                

                resenja[i] = nadjiRazmak(a[i], b[i]);

            }
            for (byte i = 0; i < t; i++)
            {
                if (resenja[i])
                {
                    Console.WriteLine("1");
                }
                else
                {
                    Console.WriteLine("0");
                }
            }
        }

        static bool nadjiRazmak(long[] a, long[] b)
        {
            long[] x = new long[a.Length - 1];
            for (int i = 0; i < a.Length-1; i++)
            {
                
                x[i] = a[i + 1] - a[i];

            }

            Array.Sort(x);

            for (int i = 0; i < x.Length-2; i++)
            {
                if (x[i+1]%x[i]!=0)
                {
                    return false;
                }
            }

         
            
            for (int i = 0; i < b.Length; i++)
            {
                int bp = BinarnaPretraga(a, b[i]);
               
                if (a[bp]==b[i])
                {
                    return false;
                }
                else if ((a[bp] - b[i]) % x[0] == 0)
                {
                    return false;
                }
            }
            return true;
        }

        static int BinarnaPretraga(long[] a,long x)
        {
            int l = 0;
            int d = a.Length;
            int s = 0;
           
            while(l<d)
            {
                s = l + (d - l) / 2;
                if (a[s]>x)
                {
                    d = s-1;
                }
                else
                {
                    l = s+1;
                }

            }

            return s;
        }

        
    }
}