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

public class Program
{
    class Takmicar
    {
        public long x;
        public long[] prihvacene;
        public long[] odbijene;
    }

    public static void Main()
    {
        long t = long.Parse(Console.ReadLine());

        List<Takmicar> takmicari = new List<Takmicar>();
        for (int i = 0; i < t; i++)
        {
            Console.ReadLine(); // Ne treba mi...
            takmicari.Add(new Takmicar()
            {
                    prihvacene = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(),
                    odbijene = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray()
            });
        }

        List<bool> results = new List<bool>();
        foreach (var takmicar in takmicari)
        {
            if (takmicar.prihvacene.Length < 2)
            {
                results.Add(false);
                continue;
            }

            long last = takmicar.prihvacene[0];
            bool result = true;
            for (int i = 1; i < takmicar.prihvacene.Length; i++)
            {
                long year = takmicar.prihvacene[i];

                if(takmicar.x == 0)
                {
                    takmicar.x = year - last;
                }
                else
                {
                    long newX = year - last;
                    if(newX == takmicar.x || takmicar.x % newX == 0 || newX % takmicar.x == 0)
                    {
                        continue;
                    }
                    else
                    {
                        result = false;
                        break;
                    }
                }
            }

            if(result == false)
            {
                results.Add(false);
                continue;
            }

            last = takmicar.prihvacene[0];
            long firstAccepted = takmicar.prihvacene.Min();
            for (int i = 1; i < takmicar.odbijene.Length; i++)
            {
                long year = takmicar.odbijene[i];
                long dif = Math.Abs(year - firstAccepted);

                if (dif == 0 || dif % takmicar.x == 0)
                {
                    result = false;
                    break;
                }
            }

            results.Add(result);
        }

        foreach (var result in results)
        {
            Console.WriteLine(result ? "1" : "0");
        }
    }
}