program ladia;
var a,b,i,g : longint;
begin
assign(input,'ROOK.IN');reset(input);
readln(a);
for g:=1 to a do begin
readln(b,i);
if (b>0) or (i>0) then begin
if (b=1) and  (i=1) then writeln('0')
else
if (b=1) and (i<>1) or (i=1) and (b<>1) then writeln('1')
else writeln('2');
end;
end;
end.