{$APPTYPE CONSOLE}
var f:text;
x,T,N,M:longint;
i:integer;
begin
assign (f,'ROOK.in');
reset(f);
read (f, T);
for i:=1 to T do begin
  readln (f, M,N);
if (M=1) and (N=1) then x:=0 else x:=2;
if (N=1) and (M>1) and (M<1000000000) or (M=1) and (1<N) and (M<1000000000)
then x:=1;
T:=T-1;
writeln (x);
end;
end.


