using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.SymbolStore;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.WebSockets;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;

namespace ConsoleApplication2
{

    class Program
    {      
        static long check(List<long> a, List<long> a1,long r)
        {
            long g = a[0] % r;
            for(long i=g;i<=a1.Max();i+=r)
            {
                if (a1.BinarySearch(i) >= 0)
                    return 0;
            }
            return 1;
        }
        static void Main(string[] args)
        {
            int f = 0,l=0;
            List<long> a = new List<long>();
            List<long> a1 = new List<long>();

            List<long> b = new List<long>();
            int t = int.Parse(Console.ReadLine());
            for (int i = 0; i < t; i++)
            {
                var s = Console.ReadLine().Split();
                int n = int.Parse(s[0]);
                int m = int.Parse(s[1]);
                var s1 = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                {
                    a.Add(long.Parse(s1[j]));
                }
                var s2 = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                   a1.Add(long.Parse(s2[j]));
                }
                a.Sort();
                long r = a[1] - a[0];
                if(check(a,a1,r)==0)
                {
                    b.Add(0);
                    a.Clear();
                    a1.Clear();
                    l = 1;
                }
                else if (n != 2)
                {
                    for (int j = 1; j < n; j++)
                    {
                        if (a[j] % r != a[j - 1] % r)
                        {
                            b.Add(0);
                            a.Clear();
                            a1.Clear();
                            f = 1;
                            break;
                        }
                    }
                }
                if (f != 1 && l!=1)
                {
                    b.Add(1);
                    a.Clear();
                    a1.Clear();
                }
                f = 0;
                l = 0;
            }
            for (int i = 0; i < b.Count; i++)
                Console.WriteLine(b[i]);
        }
    }
    
}