#include <iostream>
#include <vector>
#define forn(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int oo = 8e4+7;
int n, q, p[oo];
vector<int> con[oo];
bool ok[oo];
void dist(int v, int p, int tdist, int& maxdist) {
    if (ok[v]) maxdist = max(maxdist, tdist);
    for (int e: con[v]) if (e != p) dist(e, v, tdist + 1, maxdist);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    forn(i, n) cin >> p[i], p[i]--;
    forn(i, n-1) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        con[a].push_back(b);
        con[b].push_back(a);
    }
    cin >> q;
    while (q--) {
        int qt;
        cin >> qt;
        if (qt == 1) {
            int a, b;
            cin >> a >> b;
            a--, b--;
            swap(p[a], p[b]);
        } else {
            int l, r, x;
            cin >> l >> r >> x;
            l--, r--, x--;
            forn(i, n) ok[p[i]] = 0;
            for (int i = l; i <= r; i++) ok[p[i]] = 1;
            int d = 0;
            dist(x, x, 0, d);
            cout << d << endl;
        }
    }
    #ifdef CHUNGUS
    system("pause");
    #endif
}