using System;

namespace treci
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            ulong max = 0,min=0,min1=0,d=0;
            bool resenje = true;
            int[] resenja = new int[t];
            for (int i = 0; i < t; i++)
            {
                string line = Console.ReadLine();
                int n = int.Parse(line.Split()[0]);
                int m = int.Parse(line.Split()[1]);
                ulong[] n_niz = new ulong[n];
                ulong[] m_niz = new ulong[m];
                line = Console.ReadLine();
                var h = line.Split();
                
                for (int j = 0; j < h.Length; j++)
                {
                    n_niz[j] = ulong.Parse(h[j]);
                    if (n_niz[j] >= max)
                        max = n_niz[j];
                }
                min = max;
                min1 = max;
                for (int j = 0; j < n_niz.Length; j++)
                    if (min >= n_niz[j])
                        min = n_niz[j];
                for (int j = 0; j < n_niz.Length; j++)
                    if (min1 >= n_niz[j] && n_niz[j] != min)
                        min1 = n_niz[j];
                line = Console.ReadLine();
                d = min1 - min;
                h = line.Split();
                for (int j = 0; j < h.Length; j++)
                {
                    m_niz[j] = ulong.Parse(h[j]);
                    if ((m_niz[j] - min) % d ==0)
                        resenje = false;
                }
                if (!resenje)
                {
                    resenja[i] = 0;
                    resenje = true;
                    continue;
                }
                else
                    resenja[i] = 1;
            }
            foreach (int x in resenja)
                Console.WriteLine(x);
        }
    }
}