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

namespace Zalbe
{
    class Program
    {
        static Int64 nzd(Int64 a, Int64 b)
        {
            if (a == 0)
                return b;
            return nzd(b % a, a);
        }
        static Int64 nizNzd(Int64[] a, int n)
        {
            Int64 r = a[0];
            int i;
            for (i = 1; i < n; i++)
            {
                r = nzd(a[i], r);
                if (r == 1)
                {
                    return 1;
                }
            }
            return r;
        }
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            int[] T = new int[t];
            int i, j, br = 0;
            Int64 x, r;
            for (i = 0; i < t; i++)
            {
                var b = Console.ReadLine().Split();
                int n = int.Parse(b[0]);
                int m = int.Parse(b[1]);
                Int64[] N = new Int64[n];
                Int64[] M = new Int64[m];
                Int64[] N1 = new Int64[n];
                T[i] = 1;
                b = Console.ReadLine().Split();
                for (j = 0; j < n; j++)
                {
                    N[j] = Int64.Parse(b[j]);
                }
                b = Console.ReadLine().Split();
                for (j = 0; j < m; j++)
                {
                    M[j] = Int64.Parse(b[j]);
                }
                Array.Sort(N);
                Array.Reverse(N);
                Array.Sort(M);
                Array.Reverse(M);
                for (j = 0; j < n - 1; j++)
                {
                    N1[j] = N[j] - N[j + 1];
                    br++;
                }
                x = nizNzd(N1, br);
                if (x == 1)
                {
                    T[i] = 0;
                }
                else
                {
                    Array.Reverse(N);
                    for (j = 0; j < m; j++)
                    {
                        if (M[j] > N[0])
                        {
                            r = M[j] - N[0];
                        }
                        else
                        {
                            r = N[0] - M[j];
                        }
                        if (r % x == 0)
                        {
                            T[i] = 0;
                            break;
                        }
                    }
                }
                br = 0;
            }
            for (i = 0; i < t; i++)
            {
                Console.WriteLine(T[i]);
            }
            }
        }
    }