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

namespace _C_Zalbe
{
    class Program
    {
        static void Main(string[] args)
        {

            int t;

            List<int> n = new List<int>();
            List<int> m = new List<int>();

            List<long> n1 = new List<long>();
            List<long> m1 = new List<long>();

            List<long> r = new List<long>();

            List<int> rez = new List<int>();

            t = int.Parse(Console.ReadLine());

            for (int i = 0; i < t; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    var ulaz = Console.ReadLine().Split();
                    if ((j + 1) % 3 == 1)
                    {
                        n.Add(int.Parse(ulaz[0]));
                        m.Add(int.Parse(ulaz[1]));
                    }
                    else if ((j + 1 % 3 == 2))
                    {
                        for (int y = 0; y < n[i]; y++)
                        {
                            n1.Add(long.Parse(ulaz[y]));

                            if (y >= 1)
                            {
                                r.Add(Math.Abs(n1[y] - n1[y - 1]));
                            }
                        }
                    }
                    else
                    {
                        for (int y = 0; y < m[i]; y++)
                        {
                            m1.Add(long.Parse(ulaz[y]));
                        }
                    }
                }

                int l = 0;

                for (int k = 2; k <= r.Min(); k++)
                {
                    for (int z = 0; z < r.Count; z++)
                    {
                        if (r[z] % k != 0)
                        {
                            l = 1;
                            break;
                        }
                    }

                    if (l == 0)
                    {
                        for (int p = 0; p < m1.Count; p++)
                        {
                            if (m1[p] % k == n1[0] % k)
                            {
                                l = 1;
                                break;
                            }
                        }
                    }
                    if (l == 0)
                    {
                        rez.Add(1);
                        break;
                    }
                    l = 0;

                }
                if (rez.Count < i + 1) 
                {
                    rez.Add(0);
                }

                n1.Clear();
                m1.Clear();
                r.Clear();

            }





            for (int o = 0; o < rez.Count; o++)
            {
                Console.WriteLine(rez[o]);
            }
        }
    }
}