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, n, m;

            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++)
            {
                    var ulaz = Console.ReadLine().Split();
                    n = int.Parse(ulaz[0]);
                    m = int.Parse(ulaz[1]);

                    ulaz = Console.ReadLine().Split();
                    for (int y = 0; y < n; y++)
                    {
                        n1.Add(long.Parse(ulaz[y]));

                        if (y >= 1)
                        {
                            r.Add(Math.Abs(n1[y] - n1[y - 1]));
                        }
                    }

                    ulaz = Console.ReadLine().Split();
                    for (int y = 0; y < m; 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]);
            }
        }
    }
}