var a,b,t,i:longint;
begin
assign(input, 'rook.in');
reset(input);
readln(t);
for i:=1 to t do begin;
read (a,b);
if (a=1) and (b=1) then writeln (0) else
if (a=1) or (b=1) then writeln (1) else
if (a mod 2=0) and (b mod 2=0) then writeln (2) else 
if (a mod 2=0) and ((b+1) mod 2=0) then writeln (2) else
if ((a+1) mod 2=0) and (b mod 2=0) then writeln (2) else
if ((a+1) mod 2=0) and ((b+1) mod 2=0) then writeln (2) else
if (a mod 2=0) and (b mod 2=0) and (b mod 3=0) then writeln (1) else 

readln;
end;
end.