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

namespace ConsoleApplication2
{
    class Program
    {
        struct nizovi
        {
            public int godpr;
            public int godnepr;
            public string listapr;
            public string listanepr;
        }
        static long nzdval(long x, long y)
        {
            long nzd = 1, i, j;
            bool radi = true;
            if (x <= y)
            {
                i = x;
                while (radi && i >= 1)
                {
                  if (x % i == 0 && y % i == 0)
                    {
                      nzd = i;
                      radi = false;
                    }
                  i--;
                }
            }
            else
            {
                j = y;
                while (radi && j >= 1)
                {
                    if (x % j == 0 && y % j == 0)
                    {
                        nzd = j;
                        radi = false;
                    }
                    j--;
                }
            }
            return nzd;
        }

        static void Main(string[] args)
        {
            bool tacno = true;
            string[] p, u, s;
            int t, br = 0;
            long razlika = 0, razlika1 = 0, rez = 0;
            t = int.Parse(Console.ReadLine());
            nizovi [] takmicari = new nizovi [t];
            for (int i = 0; i < t; i++)
            {
                s = Console.ReadLine().Split();
                takmicari[i].godpr = Int32.Parse(s[0]);
                takmicari[i].godnepr = Int32.Parse(s[1]);
                takmicari[i].listapr = Console.ReadLine();
                takmicari[i].listanepr = Console.ReadLine();
            }
            for (int i = 0; i < t; i++)
            {
                tacno = true;
                rez = 0;
                p = takmicari[i].listapr.Split();
                u = takmicari[i].listanepr.Split();
                for (int j = 0; j < takmicari[i].godpr - 1; j++)
                {
                    if (j == 0)
                        razlika = Math.Abs(Int64.Parse(p[j]) - Int64.Parse(p[j + 1]));
                    else
                        razlika1 = Math.Abs(Int64.Parse(p[j]) - Int64.Parse(p[j + 1]));

                    if (j != 0 || takmicari[i].godpr == 2)
                    {
                        rez = nzdval(razlika, razlika1);
                        br++;
                        razlika = razlika1;
                    }
                }
                if (rez == 1)
                    Console.WriteLine(1);
                else
                {
                    for (int g = 0; g < takmicari[i].godnepr; g++)
                        for (int h = 0; h < takmicari[i].godpr; h++)
                            if ((Math.Abs(Int64.Parse(u[g]) - Int64.Parse(p[h]))) % rez != 0)
                                tacno = false;

                    if (tacno)
                        Console.WriteLine(0);
                    else
                        Console.WriteLine(1);
                }
            }
        }
    }
}