#include <bits/stdc++.h>
using namespace std;


void sastavi_novi(vector<int> g[], pair<int, int> e[], int n) {

    for (int i = 0; i < n-1; ++i) {

        pair<int, int> par = e[i];
        int c1 = par.first; int c2 = par.second;
        g[c1].push_back(c2);
        g[c2].push_back(c1);

    }

}


int bfs(vector<int> g[], int c, int t, int n) {

   bool *p = (bool*) calloc(n, sizeof(bool));
    queue<pair<int, int>> Q;
    p[c]=true;
    Q.push({c,0});
    if (c == t) return 0;
    while (!Q.empty()) {
        pair<int, int> pr = Q.front();
        int poz = pr.first;
        int ds = pr.second;
        Q.pop();
        for (int i = 0; i < g[poz].size(); ++i) {

            if (p[g[poz][i]] == false) {
            p[g[poz][i]]= true;
            if (g[poz][i] == t)
                return ds+1;
            Q.push({g[poz][i], ds+1});
            }
            }

        }

    return -1;

}


int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n;
    cin >> n;

    int redosled[n];

    pair<int, int> e[n-1];

    for (int i = 0; i < n; ++i) {
            int p;
            cin >> p;
    redosled[i]=p-1;
    }

    for (int i = 0; i < n - 1; ++i) {

        int p1, p2;
        cin >> p1 >> p2;
        e[i] = {p1-1,p2-1};

    }

    int q;
    cin >> q;
    for (int m = 0; m < q; ++m) {

        int u;
        cin >> u;
        if (u == 1) {
                int a,b;
        cin >> a >> b;
        a--; b--;
        int t1 = redosled[a];
        int t2 = redosled[b];

        redosled[b] = t1;
        redosled[a] = t2;

        }

        else if (u == 2) {

            int l,r,x;
            cin >> l >> r >> x;
            vector<int> g[n];

            sastavi_novi(g, e, n);

            int md = 0;

            for (int j = l-1; j < r; ++j) md = max(md, bfs(g, x-1, redosled[j], n));


            cout << md << '\n';

        }

    }

    return 0;
}