Program zadatak_5;
uses strutils;
type rastojanje = record
    rast : integer;
    pos : boolean;
    end;
var a, b : array[1..800, 1..800] of rastojanje;
    c : array[1..800] of integer;
    n, u, i, x, y, j, min, br1, br2, br3, pom, max, br : integer;
    s, q : string;
function dis(p, q : integer): rastojanje;
var dist, i : integer;
    t : rastojanje;
    u : boolean;
begin
    u := false;
    t.rast := 0;
    dist := 0;
    for i := 1 to n do begin
        if (b[p, i].rast = 1) and not b[p, i].pos then begin
            if q <> i then begin
                b[p, i].pos := true;
                t := dis(i, q);
                dist := t.rast + 1;
            end
            else u := true;
        end;
    end;
    dis.pos := u;
    if not u then dis.rast := 0
    else dis.rast := t.rast;
end;
begin
    readln(n);
    min := n;
    max := 0;
    br := 0;
    readln(s);
    repeat
        inc(br);
        u := pos(' ', s);
        val(leftstr(s, (u - 1)), c[br]);
        delete(s, 1, u);
    until s = '';
    for i := 1 to n - 1 do begin
        readln(s);
        u := pos(' ', s);
        val(leftstr(s, (u - 1)), x);
        delete(s, 1, u);
        u := pos(' ', s);
        val(leftstr(s, (u - 1)), y);
        delete(s, 1, u);
        b[x, y].rast := 1;
        b[x, y].pos := false;
    end;
    for i := 1 to n do
        for j := i + 1 to n do begin
            a[i, j] := dis(i, j);
        end;
    for i := 1 to n do
        for j := i + 1 to n do
            a[j, i] := a[i, j];
    readln(q);
    for i := 1 to length(q) do begin
        u := pos(' ', s);
        val(leftstr(s, (u - 1)), x);
        if x = 1 then begin
            delete(s, 1, u);
            u := pos(' ', s);
            val(leftstr(s, (u - 1)), br1);
            delete(s, 1, u);
            val(s, br2);
            pom := c[br1];
            c[br1] := c[br2];
            c[br2] := pom;
        end
        else begin
            u := pos(' ', s);
            val(leftstr(s, (u - 1)), br1);
            delete(s, 1, u);
            u := pos(' ', s);
            val(leftstr(s, (u - 1)), br2);
            delete(s, 1, u);
            val(s, br3);
            for j := c[br1] to c[br2] do
                writeln(a[br3, i].rast);
        end;
    end;
end.