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

namespace ConsoleApplication550
{
    class Program
    {
        static void zameni(ref long a, ref long b)
        {
            long pom = a;
            a = b;
            b = pom;
        }
        static void sortiranje(long[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                    if (a[i] < a[j]) zameni(ref a[i], ref a[j]);
            }
        }
        static void Main(string[] args)
        {
            int br = int.Parse(Console.ReadLine());
            for (int v = 0; v < br; v++)
            {
                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];
                String[] s1 = Console.ReadLine().Split();
                for (int i = 0; i < n; i++)
                {

                    a[i] = long.Parse(s1[i]);
                }
                String[] s2 = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    b[j] = long.Parse(s2[j]);
                }
                sortiranje(a, n);
                sortiranje(b, m);

               long min = 0;
                for (int i = 0; i < n - 1; i++)
                {
                    long rast = a[i + 1] - a[i];
                    if (i == 0) min = a[1] - a[0];
                    else if (min > rast) min = rast;

                }
                long x = 0;
                bool p = true;
                int k = 0;
                while (p == true && k < n)
                {
                    if (k == 0) x = a[0] % min;
                    else if (a[k] % min != x) p = false;
                    k++;
                }

                int l = 0;
                while (p == true && l < m)
                {
                    if (b[l] % min == x) p = false;
                    l++;
                }
                if (p == true) Console.WriteLine(1);
                else Console.WriteLine(0);

            }
        }
    }
}