var N,i,T,M,Q: longint;
begin
assign(INPUT, 'ROOK.IN');
reset(INPUT);
readln(T);
for i:=1 to T do
begin
readln(N,M);
if (M=1) and (N=1) then writeln(0);
if (M=N) and (M<>1) and (N<>1) then writeln(2);
if M<>N then writeln(1);
end;
end.
