#include <bits/stdc++.h>

using namespace std;

#define pii pair<int,int>
#define ll long long
#define s second
#define f first

ll n, q, t  , x, y, l, r, a, b, p[80005], d[80005], st[320005], qt[80005], qx[80005], qy[80005], qa[80005];
vector<int> g[80005];

void dfs(int v, int p){
  if(p == -1){d[v] = 0;}
  else{d[v] = d[p] + 1;}
  for(auto u : g[v]){
    if(u == p){continue;}
    dfs(u, v);
  }
}

void build(int v, int l, int r){
  //cout << v << " " << l + 1 << " " << r + 1 << " " << p[l] + 1 << " " << p[r] + 1 << endl;
  if(l == r){
    //cout << l + 1 << " " << p[l] + 1 << " " << d[p[l]] << endl;
    st[v] = d[p[l]];
    return;
  }
  int mid = (l + r) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  st[v] = max(st[2 * v], st[2 * v + 1]);
}

int maxx(int v, int l, int r, int tl, int tr){
  //cout << l + 1 << " " << r + 1 << " " << st[v] << endl;
  if(l > tr || r < tl || l > r){return -1;}
  //cout << l + 1 << " " << r + 1 << " " << st[v] << endl;
  if(tl <= l && r <= tr){return st[v];}
  int mid = (l + r) / 2;
  return max(maxx(2 * v, l, mid, tl, tr), maxx(2 * v + 1, mid + 1, r, tl, tr));
}

void update(int v, int idx, int val, int l, int r){
  if(l == r){st[v] = val; return;}
  int mid = (l + r) / 2;
  if(idx <= mid){update(2 * v, idx, val, l, mid);}
  else{update(2 * v + 1, idx, val, mid + 1, r);}
  st[v] = max(st[2 * v], st[2 * v + 1]);
}

void Q1(int id){
  swap(p[qx[id]], p[qy[id]]);
  update(1, qy[id], d[p[qy[id]]], 0, n - 1);
  update(1, qx[id], d[p[qx[id]]], 0, n - 1);
}

void Q2(int id){
  cout << maxx(1, 0, n - 1, qx[id], qy[id]) << endl;
}

void q1(int id){
  swap(p[qx[id]], p[qy[id]]);
}

void q2(int id){
  dfs(qa[id], -1);
  ll mx = 0;
  for(int i = qx[id]; i <= qy[id]; ++i){
    mx = max(d[p[i]], mx);
  }
  cout << mx << endl;
}

int main(){
  cin >> n;
  for(int i = 0; i < n; ++i){cin >> p[i]; --p[i];}
  for(int i = 0; i < n - 1; ++i){
    cin >> x >> y;
    --x; --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  cin >> q;
  bool ok = true;
  for(int i = 0; i < q; ++i){
    cin >> qt[i];
    if(qt[i] == 1){
      cin >> qx[i] >> qy[i];
      if(qx[i] == 1 || qy[i] == 1){ok = false;}
      --qx[i]; --qy[i];
    }
    else{
      cin >> qx[i] >> qy[i] >> qa[i];
      if(qa[i] != 1){ok = false;}
      --qx[i]; --qy[i]; --qa[i];
    }
  }
  if(!ok){
    for(int i = 0; i < q; ++i){
      if(qt[i] == 1){
        q1(i);
      }else{q2(i);}
    }
  }else{
    dfs(0, -1);
    build(1, 0, n - 1);
    for(int i = 0; i < q; ++i){
      if(qt[i] == 1){
        Q1(i);
      }else{Q2(i);}
    }
  }
  return 0;
}