#include <bits/stdc++.h>
#define ll long long
#define MAXN 80010
using namespace std;
ll p[MAXN];
vector<vector<ll> > G(MAXN);
vector<bool> V(MAXN);
ll ans;
void DFS(ll i,ll k,ll d)
{
    V[i]=true;
    if (i==k)
    {
        ans=max(ans,d);
        return;
    }
    for (ll next : G[i])
    {
        if (!V[next])
        {
            DFS(next,k,d+1);
        }
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,q,u,v,t,a,b,l,r,x;
    cin >> n;
    for (ll i=1;i<=n;i++)
        cin >> p[i];
    for (ll i=1;i<n;i++)
    {
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    cin >> q;
    while (q--)
    {
        cin >> t;
        if (t==1)
        {
            cin >> a >> b;
            swap(p[a],p[b]);
            continue;
        }
        ans=LLONG_MIN;
        cin >> l >> r >> x;
        for (ll i=l;i<=r;i++)
        {
            fill(V.begin(),V.end(),false);
            DFS(x,p[i],0);
        }
        cout << ans << "\n";
    }
    return 0;
}