#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];
int distp[MAXN];
vector<int> a[MAXN];
bool pos[MAXN];
int seg[4*MAXN];

void update(int node, int l, int r, int idx, int x)
{
    if(l == r)
    {
        seg[node] = x;
        return;
    }
    int mid = (l + r) / 2;
    if(idx <= mid) update(2*node, l, mid, idx, x);
    else update(2*node+1, mid+1, r, idx, x);
    seg[node] = max(seg[2*node], seg[2*node+1]);
    return;
}

int query(int node, int l, int r, int L, int R)
{
    if(L <= l && r <= R)
    {
        return seg[node];
    }
    int mid = (l + r) / 2;
    int res = 0;
    if(L <= mid) res = query(2*node, l, mid, L, R);
    if(R > mid) res = max(res, query(2*node+1, mid+1, r, L, R));
    return res;
}

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])
                {
                    pos[v] = true;
                    noviFront.push_back(v);
                    dist[v] = level;
                    //cout << v+1 << " " << dist[v] << ", ";
                }
            }
        } //cout << endl;
        level++;
        frontV = noviFront;
    }
    //cout << "END" << endl;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> p[i]; p[i]--;
    }
    bool linija = true;
    bool indx = 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; if(q[i].x != 1){ indx = false;} 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]);
                /*for(int j = 0; j < n; j++)
                {
                    cout << p[j] << " ";
                } cout << endl;*/
            }
            else
            {
                for(int j = 0; j < n; j++) pos[j] = false;
                BFS(q[i].x);
                int res = 0;
                /*cout << "dist: ";
                for(int j = 0; j < n; j++)
                {
                    cout << dist[j] << " ";
                }cout << endl;*/
                for(int j = q[i].l; j <= q[i].r; j++)
                {
                    //cout << j + 1 << " " << p[j] + 1 << " " << dist[p[j]] << " | ";
                    res = max(dist[p[j]], res);
                } //cout << endl;
                cout << res << endl;
            }
        }
    }
    else if(indx)
    {
        //cout << "seg" << endl;
        BFS(0);
        for(int i = 0; i < n; i++)
        {
            update(1, 1, n, i+1, dist[p[i]]);
            distp[i] = dist[p[i]];
        }
        for(int i = 0; i < n; i++)
        {
            dist[i] = distp[i];
        }
        for(int i = 0; i < m; i++)
        {
            if(q[i].t == 1)
            {
                update(1, 1, n, q[i].l + 1, dist[q[i].r]);
                update(1, 1, n, q[i].r + 1, dist[q[i].l]);
                swap(dist[q[i].r], dist[q[i].l]);
                //swap(p[q[i].r], p[q[i].l]);
            }
            else
            {
                cout << query(1, 1, n, q[i].l + 1, q[i].r + 1) << endl;
            }
        }

    }


    return 0;
}



/**
6
2 3 5 1 6 4
1 2
1 3
2 4
2 5
3 6
6
2 2 3 1
1 1 3
2 2 3 1
2 4 5 1
1 5 3
2 4 5 1



*/