#define here cerr<<"---------------------------\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
using namespace std;
#define maxn 80005
ll n,q;
vector<ll> g[maxn];
ll a[maxn];
struct upit{
    ll tip,l,r,x;
};
ll d[maxn];
void dfs(ll u,ll par){
    for(ll s : g[u]){
        if(s==par) continue;
        d[s] = d[u]+1;
        dfs(s,u);
    }
}
ll t[4*maxn];
void init(ll v,ll tl,ll tr){
    if(tl==tr){
        t[v] = d[tl];
        return;
    }
    ll mid = (tl+tr)/2;
    init(2*v,tl,mid);
    init(2*v+1,mid+1,tr);
    t[v] = max(t[2*v],t[2*v+1]);
}
void upd(ll v,ll tl,ll tr,ll i,ll x){
    if(tl==tr){
        t[v] = x;
        return;
    }
    ll mid = (tl+tr)/2;
    if(i<=mid) upd(2*v,tl,mid,i,x);
    else upd(2*v+1,mid+1,tr,i,x);
    t[v] = max(t[2*v],t[2*v+1]);
}
ll get(ll v,ll tl,ll tr,ll l,ll r){
    if(l>r) return 0;
    if(tl==l&&tr==r) return t[v];
    ll mid = (tl+tr)/2;
    return max(get(2*v,tl,mid,l,min(mid,r)),get(2*v+1,mid+1,tr,max(l,mid+1),r));
}
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> n;
    for(ll i = 1;i<=n;i++) cin >> a[i];
    for(ll i = 1;i<=n-1;i++){
        ll x,y; cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }
    cin >> q;
    bool e = 1;
    vector<upit> qu;
    ll cnt1 = 0;
    for(ll i = 0;i<q;i++){
        ll tip; cin >> tip;
        if(tip==1){
            ll l,r; cin >> l >> r;
            qu.pb({tip,l,r,-1});
            cnt1++;
        }else{
            ll l,r,x; cin >> l >> r >> x;
            qu.pb({tip,l,r,x});
            if(x!=1) e = 0;
        }
    }
    if(n<=4000&&q<=4000||(e==0)){
        for(upit x : qu){
            ll l = x.l;
            ll r = x.r;
            if(x.tip==1){
                swap(a[l],a[r]);
            }else{
                fill(d,d+n+1,0);
                dfs(x.x,x.x);
                ll ans = 0;
                for(ll i = l;i<=r;i++) ans = max(d[a[i]],ans);
                cout<<ans<<endl;
            }
        }
        return 0;
    }
    if(e){
        dfs(1,1);
        init(1,1,n);
        for(upit x : qu){
            ll l = x.l;
            ll r = x.r;
            if(x.tip==1){
                ll x1 = d[l];
                ll x2 = d[r];
                upd(1,1,n,r,x1);
                upd(1,1,n,l,x2);
            }else{
                cout<<get(1,1,n,l,r)<<endl;
            }
        }
        return 0;
    }
	return 0;
}