program Zalbe;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };
  var t, i, j, n, m, x, br: longint;
      a, b: array[1..75000] of int64;
      c: array[1..5] of longint;
      s: boolean;
begin
  readln(t);
  for i:=1 to t do
    begin
      c[i]:=0;
      read(n);
      read(m);
      for j:=1 to n do read(a[j]);
      for j:=1 to m do read(b[j]);
      x:=ABS(a[1]-a[2]);
      for j:=2 to (x div 2) do
        if (x mod j)=0 then
          begin
            s:=true;
            for br:=2 to n-1 do
              if (ABS(a[br]-a[br+1]) mod j)<>0 then s:=false;
            for br:=1 to m-1 do
              if (ABS(b[br]-b[br+1]) mod j)=0 then s:=false;
            if s then c[i]:=1;
          end;
      s:=true;
      for br:=2 to n-1 do
        if (ABS(a[br]-a[br+1]) mod x)<>0 then s:=false;
      for br:=1 to m-1 do
        if (ABS(b[br]-b[br+1]) mod x)=0 then s:=false;
      if s then c[i]:=1;
    end;
  for i:=1 to t do writeln(c[i]);
end.