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

namespace zalbe
{
    class Program
    {
        static bool deli(long k, long[] r, int n)
        {
            bool b = true;
            for (int i = 0; i < n; i++) if (r[i] % k != 0) b = false;
            return b;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] n = new int[t];
            int[] m = new int[t];
            List<long>[] a = new List<long>[t];
            List<long>[] b = new List<long>[t];
            for (int i=0; i< t; i++)
            {
                string[] x = Console.ReadLine().Split();
                n[i] = int.Parse(x[0]);
                m[i] = int.Parse(x[1]);
                string[] y = Console.ReadLine().Split();
                string[] z = Console.ReadLine().Split();
                a[i] = new List<long>();
                b[i] = new List<long>();
                for (int j=0; j<n[i]; j++)
                {
                    long c = long.Parse(y[j]);
                    a[i].Add(c);
                }
                for (int k=0; k<m[i]; k++)
                {
                    long c = long.Parse(z[k]);
                    b[i].Add(c);
                }
                a[i].Sort();
                b[i].Sort();
            }

            for (int i=0; i< t; i++)
            {
                bool p = true;
                long minr;
                long[] r = new long[n[i] - 1];
                minr = a[i][1] - a[i][0];
                r[0] = minr;
                for (int j=2; j<n[i]; j++)
                {
                    r[j - 1] = a[i][j] - a[i][j - 1];
                    minr = Math.Min(minr, r[j - 1]);
                }
                long d = minr;
                bool c = true;
                long k = minr;
                while (k>1 && c)
                {
                    if (deli(k,r,n[i]-1))
                    {
                        d = k;
                        c = false;
                    }
                    k--;
                }
                for (int j = 0; j < n[i] - 1; j++) if (r[j] % d != 0) p = false;
                if (p)
                {
                    for (int j = 0; j < m[i]; j++) if (Math.Abs((double)a[i][n[i] - 1] - b[i][j]) % d == 0) p = false;
                }
                if (p) Console.WriteLine(1);
                else Console.WriteLine(0);
            }
        }
    }
}