//SRC is locked
var i,l,r,t,cccum:longint;
ltk,w,size:array[1..30000] of longint;
c:char;
//SRC is locked
function f(x:longint):longint;
var s:longint;
begin
if ltk[x]=x then exit(x);
s:=ltk[x];
ltk[x]:=f(ltk[x]);
w[x]:=w[x]+w[s];
exit(ltk[x]);
end;
//SRC is locked
procedure process_1;
begin
readln(l,r);
w[f(l)]:=size[f(r)];
size[ltk[r]]:=size[ltk[r]]+size[ltk[l]];
ltk[ltk[l]]:=ltk[r];
end;
//SRC is locked
procedure process_2;
begin
inc(cccum);
readln(l,r);
if f(l)=f(r) then write(abs(w[l]-w[r])-1) else write(-1);
if cccum mod 10000=0 then writeln;
end;
//SRC is locked
begin
for i:=1 to 30000 do
begin
ltk[i]:=i;size[i]:=1;
end;
readln(t);
cccum:=0;
for i:=1 to t do
begin
read©;
if c='M' then process_1;
if c='C' then process_2;
end;
end.
//SRC is locked
var i,l,r,t,cccum:longint;
ltk,w,size:array[1..30000] of longint;
c:char;
//SRC is locked
function f(x:longint):longint;
var s:longint;
begin
if ltk[x]=x then exit(x);
s:=ltk[x];
ltk[x]:=f(ltk[x]);
w[x]:=w[x]+w[s];
exit(ltk[x]);
end;
//SRC is locked
procedure process_1;
begin
readln(l,r);
w[f(l)]:=size[f(r)];
size[ltk[r]]:=size[ltk[r]]+size[ltk[l]];
ltk[ltk[l]]:=ltk[r];
end;
//SRC is locked
procedure process_2;
begin
inc(cccum);
readln(l,r);
if f(l)=f(r) then write(abs(w[l]-w[r])-1) else write(-1);
if cccum mod 10000=0 then writeln;
end;
//SRC is locked
begin
for i:=1 to 30000 do
begin
ltk[i]:=i;size[i]:=1;
end;
readln(t);
cccum:=0;
for i:=1 to t do
begin
read©;
if c='M' then process_1;
if c='C' then process_2;
end;
end.
//SRC is locked