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

namespace Zalbe
{
    class Program
    {
        static long[] Prebaci(string[] str)
        {
            long[] a = new long[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                a[i] = long.Parse(str[i]);
            }
            return a;
        }
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            int[] kon = new int[T];
            for (int u = 0; u < T; u++)
            {
                bool p = true;
                string[] str = Console.ReadLine().Split();
                int N = int.Parse(str[0]);
                int M = int.Parse(str[1]);
                str = Console.ReadLine().Split();
                long[] a = new long[N];
                a = Prebaci(str);
                Array.Sort(a);
                long poc = a[0];
                long X = a[N - 1];
                for (int i = 0; i < N - 1; i++)
                {
                    X = Math.Min(a[i + 1] - a[i], X);
                    if (X == 1)
                    {
                        p = false;
                        break;
                    }
                }
                if (p)
                {
                    for (int i = 1; i < N; i++)
                    {
                        if (Math.Abs(long.Parse(str[i]) - poc) % X != 0)
                        {
                            p = false;
                            break;
                        }
                    }
                }
                str = Console.ReadLine().Split();
                if (p)
                {
                    for (int i = 0; i < M; i++)
                    {
                        if (Math.Abs(long.Parse(str[i]) - poc) % X == 0)
                        {
                            p = false;
                            break;
                        }
                    }
                }
                if (p) kon[u] = 1;
                else kon[u] = 0;
            }
            for (int i = 0; i < T; i++)
            {
                Console.WriteLine(kon[i]);
            }
        }
    }
}