program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

var
  t,i,m,n,br,j,k,x,r,re,o,l,u:integer;
  a,b,c,res:array [1..100] of integer;

begin
  readln(t);
  for br:=1 to t do
  begin
  readln(n, m);
  for i:=1 to n do readln(A[i]);
  for j:=1 to m do readln(B[j]);
  for k:=1 to n do C[k]:=a[k];
  for u:=n+1 to m do C[u]:=b[u];
  For i := n+m-1 DownTo 1 do
  For j := 2 to i do
  If (c[j-1] > c[j]) Then
     Begin
     x := c[j-1];
     c[j-1] := c[j];
     c[j] := x;
     end;
  re:=1;
  r:=A[2]-A[1];
  for l:=1 to n+m do
     begin
     if(A[l+1]-A[l]<>r) then re:=0;
     end;
  Res[br]:=re;
  end;
  for o:=1 to t do writeln(Res[o]);
  readln;
end.