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

namespace ConsoleApp3
{
    class Program
    {
        static long nzd(long a, long b)
        {
            if (a == 0) return b;
            return nzd(b % a, a);
        }
        static long nzdniz(long[] a)
        {
            long b = a[0];
            for (int i=1; i<a.Length; i++)
            {
                b = nzd(b, a[i]);
                if (b == 1)
                    return 1;
            }
            return b;
        }
        static bool resi3(long[] b, long[] a, long x)
        {
            for (int i=0; i<b.Length; i++)
            {
                if ((a[0] - b[i]) % x == 0)
                    return false;
            }
            return true;
        }
        static void resi2(long[] b, long[] a, long nzd, ref bool b1)
        {
            for (long i = 2; i <=nzd; i++)
            {
                if (nzd % i == 0)
                {
                    b1 = resi3(b, a, i);
                }
                if (b1)
                    break;
            }
        }
        static void resi(ref bool b1)
        {
            string[] s = Console.ReadLine().Split(' ');
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            long[] a = new long[n];
            long[] b = new long[m];
            s = Console.ReadLine().Split(' ');
            for (int i = 0; i < n; i++)
                a[i] = long.Parse(s[i]);
            s = Console.ReadLine().Split(' ');
            for (int i = 0; i < m; i++)
                b[i] = long.Parse(s[i]);
            Array.Sort(a);
            Array.Sort(b);
            long[] a1 = new long[n - 1];
            for (int i = 0; i < n - 1; i++)
                a1[i] = a[i + 1] - a[i];
            Array.Sort(a1);
            long nzd = nzdniz(a1);
            if (nzd != 1)
                resi2(b, a, nzd, ref b1);
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] b11 = new int[t];
            for (int i = 0; i < t; i++)
            {
                bool b1 = false;
                resi(ref b1);
                if (b1) b11[i] = 1;
                else b11[i] = 0;
            }
            for (int i = 0; i < t; i++)
                Console.WriteLine(b11[i]);
        }
    }
}