#include <bits/stdc++.h>
#define INF 1000000000
#define MAXN 80010
using namespace std;

struct unos
{
    int t;
    int l, r, x;
};
unos q[MAXN];
int p[MAXN];
int indeg[MAXN];
int dist[MAXN];
vector<int> a[MAXN];
bool pos[MAXN];

void BFS(int s)
{
    //cout << "START " << s+1 << endl;
    vector<int> frontV;
    frontV.push_back(s);
    pos[s] = true;
    dist[s] = 0;
    //cout << s + 1 << endl;
    int level = 1;
    while(!frontV.empty())
    {
        vector<int> noviFront;
        for(auto u : frontV)
        {
            for(auto v : a[u])
            {
                if(!pos[v])
                {
                    //cout << v+1 << " ";
                    pos[v] = true;
                    noviFront.push_back(v);
                    dist[v] = level;
                }
            }
        } //cout << endl;
        level++;
        frontV = noviFront;
    }
    //cout << "END" << endl;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> p[i];
    }
    bool linija = true;
    for(int i = 0; i < n-1; i++)
    {
        int u, v;
        cin >> u >> v;
        u--, v--; indeg[u]++;indeg[v]++; if(indeg[u] > 2 || indeg[v] > 2) linija = false;
        a[u].push_back(v);
        a[v].push_back(u);
    }
    int m;
    cin >> m;
    for(int i = 0; i < m; i++)
    {
        cin >> q[i].t >> q[i].l >> q[i].r; q[i].l--; q[i].r--;
        if(q[i].t == 2) {cin >> q[i].x; q[i].x--;}
    }
    if(n <= 4000 && m <= 4000)
    {
        for(int i = 0; i < m; i++)
        {
            if(q[i].t == 1) swap(p[q[i].l], p[q[i].r]);
            else
            {
                for(int j = 0; j < n; j++) pos[j] = false;
                BFS(q[i].x);
                int res = 0;
                for(int j = q[i].l; j <= q[i].r; j++)
                {
                    res = max(dist[p[j]], res);
                }
                cout << res << endl;
            }
        }
    }


    return 0;
}