{$APPTYPE CONSOLE}

var
a,b,c,n:longint;
begin
assign (input, 'ROOK.IN');
reset (input);
read(n);
while 1<=n do begin
read(a,b);
if (a mod 2=1 ) and (b mod 2=1) then c:=2;
if (a mod 2=0) and (b mod 2=0 ) then c:=2;
if (a mod 2=1) and (b mod 2=0) then c:=1;
if  (a mod 2=0) and (c mod 2=1) then c:=1;
if (a=1) or (b=1) then c:=1;
if (a=1) and (b=1) then c:=0;

writeln(c);
dec(n);
end;
end.
