#include<bits/stdc++.h>
using namespace std;

#define INFINITY 9999
#define max 500

int niz[max];

void dijkstra(int G[max][max],int n,int startnode)
{
   int cost[max][max],distance[max],pred[max];
   int visited[max],count,mindistance,nextnode,i,j;
   for(i=0;i<n;i++)
      for(j=0;j<n;j++)
   if(G[i][j]==0)
      cost[i][j]=INFINITY;
   else
      cost[i][j]=G[i][j];
   for(i=0;i<n;i++) {
      distance[i]=cost[startnode][i];
      pred[i]=startnode;
      visited[i]=0;
   }
   distance[startnode]=0;
   visited[startnode]=1;
   count=1;
   while(count<n-1) {
      mindistance=INFINITY;
      for(i=0;i<n;i++)
         if(distance[i]<mindistance&&!visited[i]) {
         mindistance=distance[i];
         nextnode=i;
      }
      visited[nextnode]=1;
      for(i=0;i<n;i++)
         if(!visited[i])
      if(mindistance+cost[nextnode][i]<distance[i]) {
         distance[i]=mindistance+cost[nextnode][i];
         pred[i]=nextnode;
      }
      count++;
   }
   for(i=0;i<n;i++)
   {
        if(i!=startnode)
        {
             niz[i]=distance[i];
        }
        if (i==startnode)
        {
            niz[i]=0;
        }
   }
}


int main()
{
    int n,a,b;
    cin>>n;
    int redosled[n];
    int graf[n][max];
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        {
            graf[i][j]=0;
        }
    }
    for (int i=0;i<n;i++)
    {
        cin>>a;
        redosled[i]=a;
    }
    for (int i=0;i<n-1;i++)
    {
        cin>>a>>b;
        graf[a-1][b-1]=1;
        graf[b-1][a-1]=1;
    }
    /*
    cout<<endl<<endl;
    for (int i=0;i<n;i++)
    {
        cout<<i+1<<"     ";
        for (int j=0;j<n;j++)
        {
            cout<<graf[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<endl;
    */
    int u=0;
    int k,c,l,r,x;
    cin>>k;
    for (int i=0;i<k;i++)
    {
        cin>>a;
        if (a==1)
        {
            cin>>b>>c;
            int pom;
            pom=redosled[b-1];
            redosled[b-1]=redosled[c-1];
            redosled[c-1]=pom;
        }
        else if (a==2)
        {
            cin>>l>>r>>x;
            int clan;
            //int clan=redosled[x-1]-1;
            //cout<<clan<<"    ";
            dijkstra(graf,n,x-1);
            /*
            for (int j=0;j<n;j++)
            {
                cout<<niz[j]<<" ";
            }
            */
            int maksi=0;
            for(int i=l;i<=r;i++)
            {
                clan=redosled[i-1]-1;
                int ras=niz[clan];
                //cout<<clan<<"  "<<ras<<"         ";
                if (ras>maksi)
                {
                    maksi=ras;
                }
            }
            cout<<maksi<<endl;
        }
    }
}