using Microsoft.VisualBasic.CompilerServices;
using System;
using System.ComponentModel.DataAnnotations;

namespace Zalbe
{
    class Program
    {
        static long NajmanjaRazlika(long[] niz)
        {
            long min = long.MaxValue;
            for(int i = 0; i < niz.Length - 1; i++)
            {
                for (int j = i + 1; j < niz.Length; j++)
                {
                    if (Math.Abs(niz[i] - niz[j]) < min) min = Math.Abs(niz[i] - niz[j]);
                }
            }
            return min;
        }
        static bool Razlika(long a, long b, long x)
        {
            long s = Math.Abs(a - b);
            if (s % x == 0) return true;
            else return false;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] ispisi = new int[t]; int g = 0;

            for (int i = 0; i < t; i++)
            {
                string[] s = Console.ReadLine().Split();
                int n = int.Parse(s[0]);
                int m = int.Parse(s[1]);
                long[] niz_a = new long[n];
                long[] niz_b = new long[m];
                string[] o = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                {
                    niz_a[j] = long.Parse(o[j]);
                }
                string[] w = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    niz_b[j] = long.Parse(w[j]);
                }
              
                long min = NajmanjaRazlika(niz_a);
                bool p = false; bool ispis = false ;
                for(int j = 0; j < n - 1; j++)
                {
                    p = Razlika(niz_a[j], niz_a[j + 1], min);
                }

                if(p)
                {
                    for(int j = 0; j < m; j++)
                    {
                        for(int k = 0; k < n; k++)
                        {
                            p = Razlika(niz_b[j], niz_a[k], min);
                            if (p && ispis == false) { ispisi[g] = 0 ; g++; ispis = true; }
                        }
                        
                    }
                    if (!ispis) { ispisi[g] = 1; g++; }
                }
                else { ispisi[g] = 0; g++; }
            }

            for(int i = 0; i < t; i++)
            {
                Console.WriteLine(ispisi[i]);
            }
        }
    }
}