Program rook;
Var
d,e,i,t,m,n:integer;
Begin
 assign(input,'ROOK.in'); reset(input);
 Read(t);
 for i:=1 to t do
  Begin
 Read(n,m);
 if m=1 then d:=1;
 if n=1 then e:=1;
 if (m+n)=2 then WriteLn('0')
 else
 if n=1 then WriteLn('1')
  else
  if m=1 then Writeln('1')
   else writeLn('2');
 end;
end.