program ROOK;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  f:text;
  n, m:extended;
  i,t:integer;


begin
assign(f, 'ROOK.IN');
reset(F);
readln(f, t);

for i:=1 to t do
begin
  read(f, n,m);
  if (n=1) and (m=1) then
  writeln('0') else
  if (n=1) or (m=1) then
  writeln('1') else
  writeln('2');
end;
     //         readln;


end.
