
{$APPTYPE CONSOLE}

var f:text;
a,b,c,n:longint;
begin
assign (f, 'ROOK.IN');
reset (f);
read(f, n);
while 1<=n do begin
read(f, a,b);
if (a<>1) and (b<>1) then c:=2;
if (a<>1) and (b=1) then c:=1;
if (a=1) and (b=1) then c:=0;
if (a=1) and (b<>1) then c:=1;
n:=n-1;
writeln(c);
end;
{readln;}
end.
