program rook;

{$APPTYPE CONSOLE}

Var x,y,t,i:longint;

begin
assign(input,'rook.in ');
reset(input);
readln(t);
for i := 1 to t do
begin
  read(x,y);
  readln;
  if (x=1) and (y=1) then
  writeln('0')
  else
  if (x=1)or (y=1) then
  writeln(1)
  else writeln(2);
end;

end.
