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

#define NMAX 80005

vector<int> g[NMAX];

void bfs(int **dist, int c, int n) {

   bool *p = (bool*) calloc(n, sizeof(bool));
    queue<pair<int, int>> Q;
    p[c]=true;
    Q.push({c,0});
    dist[c][c] = 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;
            dist[c][g[poz][i]] = ds+1;
            Q.push({g[poz][i], ds+1});
            }
            }

        }

}


int main() {

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

    int n;
    cin >> n;

    int **dist = (int**)malloc(n*sizeof(int*));
    int redosled[n];

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


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

        int p1, p2;
        cin >> p1 >> p2;
        p1--; p2--;
        g[p1].push_back(p2);
        g[p2].push_back(p1);


    }

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

            bfs(dist, i, n);

    }

    /*for (int i = 0; i < n; ++i) {

        for (int j = 0; j < n; ++j) {

            printf("%d ", dist[i][j]);

        }
        printf("\n");

    }*/

    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; x--;

            int md = 0;

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


            cout << md << '\n';

        }

    }

    return 0;
}