#include <iostream>
#include <list>
#include <stdlib.h>
#include <queue>
#include <vector>
using namespace std;
void dijkstra(list<long> l[], long poc, long* d, long n)
{
  long* p=(long*)malloc(n*sizeof(long));
  for(long i=0;i<n;i++)
    p[i]=0;
  priority_queue<long, vector<long>, greater<long>> q;
  q.push(poc);
  p[poc]=1;
  while(!q.empty())
  {
    long tr=q.top();
    q.pop();
    p[tr]=1;
    for(auto i=l[tr].begin();i!=l[tr].end();i++)
      if(d[*i]>d[tr]+1)
      {
        d[*i]=d[tr]+*i;
        if(!p[*i])
          q.push(*i);
      }
  }
}
int main() 
{
  long n,q,i,u,v,a,b,l,r,x,pom,scan;
  int br;
  scanf("%ld", &n);
  long* p=(long*)malloc(n*sizeof(long));
  list<long> g[n];
  for(i=0;i<n;i++)
  {
    scanf("%ld", &scan);
    p[i]=scan-1;
  }
  for(i=0;i<n-1;i++)
  {
    scanf("%ld%ld", &u, &v);
    g[u-1].push_back(v-1);
    g[v-1].push_back(u-1);
  }
  scanf("%ld", &q);
  list<long> rez;
  for(i=0;i<q;i++)
  {
    scanf("%d", &br);
    if(br==1)
    {
      scanf("%ld%ld", &a, &b);
      pom=p[a-1];
      p[a-1]=p[b-1];
      p[b-1]=pom;
    }
    else if(br==2)
    {
      scanf("%ld%ld%ld", &l, &r, &x);
      long* d=(long*)malloc(n*sizeof(long));
      for(i=0;i<n;i++)
        d[i]=n;
      for(auto it=g[x-1].begin();it!=g[x-1].end();i++)
        d[*it]=1;
      d[x-1]=0;
      dijkstra(g,x-1,d,n);
      long max=d[l-1];
      for(i=l;i<r;i++)
      {
        if(d[p[i]]>max)
          max=d[p[i]];
      }
      rez.push_back(max);
      free(d);
      d=NULL;
    }
  }
  for(auto it=rez.begin();it!=rez.end();it++)
    printf("%ld\n", *it);
}