#include <bits/stdc++.h>

using namespace std;

vector<int>graff[800080], order;
int l, r, maks;
int p[800080];

void bfs(int v, int *dist, int pocetni, int br)
{
    for(int i=0; i<graff[v].size(); i++)
    {
        int u=graff[v][i];
        if(dist[u]==0 and u!=pocetni)
        {
            dist[u]=dist[v]+1; order.push_back(u);
            if(p[u]<=r and p[u]>=l and maks<dist[u])maks=dist[u];
        }
    }
    if(br==order.size())return;
    bfs(order[br], dist, pocetni, br+1);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    cin>>n;

    for(int i=1; i<=n; i++){int a; cin>>a; p[a]=i;}
    for(int i=0; i<n-1; i++)
    {
        int a, b;
        cin>>a>>b;
        graff[a].push_back(b); graff[b].push_back(a);
    }

    int q; cin>>q;
    vector<int>res;

    for(int i=0; i<q; i++)
    {
        int type; cin>>type;
        if(type==1)
        {
            int a, b, h; cin>>a>>b;
            h=p[a]; p[a]=p[b]; p[b]=h;
            continue;
        }
        int x, dist[n+1]={0};
        cin>>l>>r>>x;
        bfs(x, dist, x, 0);
        res.push_back(maks); maks=0; order.clear();
    }

    for(int i=0; i<res.size(); i++)cout<<res[i]<<"\n";
    return 0;
}