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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int n, m;
            long[] g1;
            long[] g2;
            string[] s;
            bool rez;
            for (int i = 0; i < t; i++) 
            {
                
                //-------------input
                s = Console.ReadLine().Split();
                n = int.Parse(s[0]);
                g1 = new long[n];
                m = int.Parse(s[1]);
                g2 = new long[m];

                s = Console.ReadLine().Split();
                for (int j = 0; j < n; j++) g1[j] = long.Parse(s[j]);

                s = Console.ReadLine().Split();
                for (int j = 0; j < m; j++) g2[j] = long.Parse(s[j]);

                //-------x-----input

                //-------------inputThings

                Array.Sort(g1);

                //-------x-----inputThings

                //-------------minRazlika

                long min = g1[g1.Length - 1];
                for (int j = n - 1; j > 0 ; j--) 
                {
                    if (min > g1[j] - g1[j - 1]) min = g1[j] - g1[j - 1];
                }

                //-------x-----minRazlika
                
                //-------------solution

                rez = true;
                for (int j = 1; j < n; j++) // g1
                {
                    if ((g1[j] - g1[0]) % min != 0) rez = false;
                }
                for (int j = 0; j < m; j++) // g2
                {
                    if ((g2[j] - g1[0]) % min == 0) rez = false;
                }
                if (rez) Console.WriteLine("1");
                else Console.WriteLine("0");

                //-------x-----solution

            }
            Console.ReadKey();
        }
    }
}