program ROOK;

{$APPTYPE CONSOLE}

uses
  SysUtils;

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

end.
