var
 t,i,m,n:longint;
begin
     assign(input,'rook.in'); reset(input);
     read(t);
     for i:=1 to t do
     begin
          read(n,m);
          if (n=1) and (m=1) then writeln('0');
          if (n=1) and (m>1) then writeln('1');
          if (n>1) and (m=1) then writeln('1');
          if (n>1) and (m>1) then writeln('2');
     end;
end.

