program rook;

{$APPTYPE CONSOLE}
var
i,x,y,t:integer;
begin
assign (input, 'ROOK.in'); reset (input) ;
 readln(t);
 if (x>=1) and (x<=10000000000) and (y>=1) and (y<=10000000000) then
  for i := 1 to t do begin
 readln(x,y);
 if (x=1) and (y=1)  then writeln(0);
 if  ((x=1) and (y>1)) or ((x>1) and (y=1))  then writeln(1);
 if   (x>1) and (y>1) then  writeln(2)
  end
  else writeln(0); 
end.

