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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());
            int N = 0;
            int M = 0;
            int[] r = new int[T];
            for (int f = 0; f < T; f++)
            {
                string[] s = Console.ReadLine().Split(' ');
                N = int.Parse(s[0]);
                M = int.Parse(s[1]);
                long[] a = new long[N];
                long[] b = new long[M];
                     string[] h = Console.ReadLine().Split(' ');
                for (int i = 0; i < N; i++)
                    a[i] = long.Parse(h[i]); 
                string[] o = Console.ReadLine().Split(' ');
                for (int i = 0; i < M; i++)
                    b[i] = long.Parse(o[i]);
                long p = 0;
                for (int i = 0; i < N - 1; i++)
                    for (int j = i + 1; j < N; j++)
                        if (a[i] > a[j])
                        {
                            p = a[i];
                            a[i] = a[j];
                            a[j] = p;
                        }
                for (int i = 0; i < M - 1; i++)
                    for (int j = i + 1; j < M; j++)
                        if (b[i] > b[j])
                        {
                            p = b[i];
                            b[i] = b[j];
                            b[j] = p;
                        }
                long x = a[1] - a[0];
                for (int i = 2; i < N; i++)
                    if (a[i] - a[i - 1] < x)
                        x = a[i] - a[i - 1];
                r[f] = 1;
                for (int i = 1; i < N; i++)
                {
                    if ((a[i] - a[0]) % x != 0)
                        r[f] = 0;
                }
                for (int i = 0; i < M; i++)
                {
                    if ((b[i] - a[0]) % x == 0)
                        r[f] = 0;
                }
            }
            for (int f = 0; f < T; f++)
                Console.WriteLine(r[f]);
        }
    }
}