{$APPTYPE CONSOLE}
var f:text;
    N,i,T,M,Q: longint;
begin
assign(f, 'ROOK.in');
reset(f);
readln(f, T);
for i:=1 to T do
begin
readln(f, N,M);
if (N<>1) and (M<>1) then Q:=2;
If (N<>1) and (M=1) then Q:=1;
if (N=1) and (M=1) then Q:=0;
If (N=1) and (M<>1) then Q:=1;
writeln(Q);
dec(n);
end;
end.
