program project3;
  type
    niz = array [1..75000] of longint;
  var
    t, i, j, k, n, m:integer;
    x, y, nzd:longint;
    a:array[1..5] of integer;
    b, razb, c:niz;
begin
    ReadLn(t);
    for i:=1 to t do a[i]:=1;
    for i:=1 to t do
    begin
       a[i]:=1;
       Read(n);
       ReadLn(m);
       for j:=1 to n do Read(b[j]);
       ReadLn();
       for j:=1 to m do Read(c[j]);
       ReadLn();
       for j:=1 to n-1 do razb[j]:=b[j]-b[j+1];
       razb[n]:=b[i]-b[n];
       nzd:=1;
       x:=abs(razb[1]);
       y:=abs(razb[2]);
       while ((x mod y > 0) and (y mod x > 0)) do
       begin
          if (x>y) then x:=x-y
          else y:=y-x;
       end;
       if (x>y) then nzd:=y
       else nzd:=x;
       for j:=1 to n do
       begin
          x:=nzd;
          y:=abs(razb[j]);
          while ((x mod y > 0) and (y mod x > 0)) do
             begin
                 if (x>y) then x:=x-y
                 else y:=y-x;
             end;
             nzd:=x;
       end;
       for j:=1 to m do if ((c[j]-b[1]) mod nzd = 0) then a[i]:=0;
       b := default(niz);
       razb := default(niz);
       c := default(niz);
    end;
    for i:=1 to t do WriteLn(a[i]);
    ReadLn();
end.