#include <bits/stdc++.h>

using namespace std;

#define ll long long
const ll MAXN=4001,logN=17,MAXA=2<<3,INF=1e9,MOD=1e9+7;
const double EPS=1e-9;
#define fi first
#define se second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define mid (l+r)/2
#define sz(a) int((a).size())
#define all(a) a.begin(),a.end()
#define mod 1000000007LL
#define pb push_back
#define endl "\n"
#define PRINT(x) cerr<<#x<<'-'<<x<<endl<<flush;
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pb push_back
#define pf push_front
#define ppf pop_front
#define ppb pop_back
#define PRINTvec(x) { cerr<<#x<<"-"; for(int i=0;i<sz(x);i++) cerr<<x[i]<<" "; cerr<<endl; }

ll n,m,k,q,x,y,z,l,r,res=INF;
string s,t;
///ifstream in;
///ofstream out;

#ifdef ONLINE_JUDGE
    #define in cin
    #define out cout
#endif

vector<int> p;
vector<int> adj[MAXN];
int dist[MAXN];

void dfs(int node, int prev) {
    for(auto v:adj[node]){
        if(v!=prev){
            dist[v]=dist[node]+1;
            dfs(v,node);
        }
    }
}

void solve(){
    cin>>n;
    p.resize(n+1);
    for(int i=1;i<=n;i++) cin>>p[i];
    for(int i=0;i<n-1;i++){
        cin>>l>>r;
        adj[l].pb(r);
        adj[r].pb(l);
    }
    cin>>q;
    while(q--){
        int tip;
        cin>>tip;
        if(tip==1){
            cin>>l>>r;
            swap(p[l],p[r]);
        }
        else{
            cin>>l>>r>>x;
            memset(dist,0,sizeof(dist));
            dfs(x,-1);
            int ret=0;
            for(int i=l;i<=r;i++){
                ret=max(ret,dist[p[i]]);
            }
            cout<<ret<<endl;
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        ///in.open("D:\\UROS\\Programiranje\\input.in",ios::in);
        ///out.open("D:\\UROS\\Programiranje\\output.out",ios::trunc | ios::out);
    #endif
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);'
    int t;
    ///cin>>t;
    t=1;
    while(t--){
        solve();
    }
    return 0;
}