#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ld long double
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(a) begin(a),end(a)
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ld, null_type,less<ld>, rb_tree_tag,tree_order_statistics_node_update>
// order_of_key -> # less than k
// find_by_order -> k-th element
// pq max element

const double eps = 0.000001;
const int modi = 1e9+7;
const int NMAX = 200010;
int p[NMAX];
int n,q;
int dist[NMAX];
vector<int>al[NMAX];

void dfs1(int node, int d) {
    if ( dist[node] != -1 ) {
        return;
    }
    dist[node] = d++;
    for (auto u : al[node] ) {
        dfs1(u,d);
    }
}

void dfs(int node) {
    for (int i = 1 ; i <= n ; i++ ) {
        dist[i] = -1;
    }
    dfs1(node, 0);
}

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

    }
    cin >> q;
    while(q--) {
        int korak;
        cin >> korak;
        if ( korak == 1 ) {
            int a,b;
            cin >> a >> b;
            swap(p[a],p[b]);
        }
        else {
            int l,r,x;
            cin >> l >> r >> x;
            dfs(x);
            int maks = 0;
            for (int i = l ; i <= r ; i++ ) {
                maks = max(maks,dist[p[i]]);
            }
            cout << maks << endl ;
        }
    }

}


int main () {
    


    
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    
    //cin >>t ;  


    int i = 1 ;
    while(t--) {
        //cout << "Case #" << i++ << ": " ;
        solve(); 
    }
}