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

namespace zalbe
{
    class Program
    {
        static int PostojiLiX(int N,int M,long[] d,long[] o)
        {
            int pos = 1;
            bool prom;
            do
            {
                prom = false;
                for (int i = 0; i < N - 1; i++)
                {
                    if (d[i] > d[i + 1])
                    {
                        long p = d[i];
                        d[i] = d[i + 1];
                        d[i + 1] = p;
                        prom = true;
                    }
                }
            } while (prom);
            long X = d[1] - d[0];
            for (int i = 1; i < N - 1; i++)
            {
                long x2 = d[i + 1] - d[i];
                if (x2 < X)
                    X = x2;
            }
            for (int i = 1; i < N; i++)
            {
                if ((d[i] - d[0]) % X != 0)
                    pos = 0;
            }
            for (int i = 0; i < M; i++)
            {
                if ((o[i] - d[0]) % X == 0)
                    pos = 0;
            }
            return pos;
        }

        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            int[] posevi = new int[T];
            for (int g = 0; g < T; g++)
            {
            string[] s = Console.ReadLine().Split();
            int N = int.Parse(s[0]);
            int M = int.Parse(s[1]);
            long[] d = new long[N];
            long[] o = new long[M];
            string[] ds = Console.ReadLine().Split();
            for (int i = 0; i < N; i++)
                d[i] = long.Parse(ds[i]);
            string[] os = Console.ReadLine().Split();
            for (int i = 0; i < M; i++)
                o[i] = long.Parse(os[i]);
            posevi[g]=PostojiLiX(N, M, d, o);
            }
            for (int i = 0; i < T; i++)
            {
                Console.WriteLine(posevi[i]);
            }
        }
    }
}