program Project1;

{$APPTYPE CONSOLE}

var
f:textfile;
r,n,i,rlt:integer;
x1,y1:LONGINT;
begin
assign(f, 'ROOK.IN');
reset(f);
readln(f,n);
for I := 1 to n do begin
    readln(F,x1,y1);
    if (x1=1) and (y1=1) then begin
       rlt:=0;
    end else begin
       if (x1=1) or (y1=1) then BEGIN rlt:=1;
       END ELSE BEGIN if (x1>1) or (y1>1) then rlt:=2;
       END;
    end;
    writeln(rlt);
end;
close(f);
end.

