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;
                    }
                }
                str = Console.ReadLine().Split();
                if (p)
                {
                    for (long i = 1; i < X; i++)
                    {
                        if (X % i == 0 && i != X)
                        {

                            long x = X / i;
                            for (int j = 1; j < N; j++)
                            {
                                if (Math.Abs(a[j] - poc) % x != 0)
                                {
                                    p = false;
                                    break;
                                }
                            }
                            if (p)
                            {
                                for (int j = 0; j < M; j++)
                                {
                                    if (Math.Abs(long.Parse(str[j]) - poc) % x == 0)
                                    {
                                        p = false;
                                        break;
                                    }
                                }
                            }
                            if (p)
                            {
                                kon[u] = 1;
                                break;
                            }
                        }
                    }
                    if(!p) kon[u] = 0;
                }
                else kon[u] = 0; 
            }
            for (int i = 0; i < T; i++)
            {
                Console.WriteLine(kon[i]);
            }
        }
    }
}