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

namespace takmicenje3
{
    class Program
    {
        static void Main(string[] args)
        {
            int t, n, m,min=0,z=1;
            t = int.Parse(Console.ReadLine());
            int[] z1 = new int[t];
            for (int i = 0; i < t; i++)
            {
                string[] nz = Console.ReadLine().Split();
                n = Int32.Parse(nz[0]);
                m = Int32.Parse(nz[1]);
                int[] niz = new int[n];
                int[] nr = new int[n-1];
                int[] niz1 = new int[m];
                string[] nz1 = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                {
                    niz[j] = Int32.Parse(nz1[j]);
                    if (j >= 1)
                    {
                        nr[j - 1] = niz[j] - niz[j - 1];
                        if (nr[j - 1] < 0)
                        {
                            nr[j - 1] *= -1;
                        }
                        if(j==1)
                        {
                            min = nr[0];
                        }
                        if (nr[j - 1] < min)
                        {
                            min=nr[j-1];
                        }
                    }
                }
                string[] nz2 = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    niz1[j] = Int32.Parse(nz2[j]);
                }
                int c = niz[0] % min;
                for (int j = 1; j < n && z==1; j++)
                {
                    if (niz[j] % min != c)
                        z--;
                }
                for (int j = 0; j < m && z == 1; j++)
                {
                    if (niz1[j] % min == c)
                        z--;
                }
                z1[i]=z;
            }
            for (int i = 0; i < t; i++)
                Console.WriteLine(z1[i]);
            Console.ReadLine();
        }
    }
}