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