using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.SymbolStore;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.WebSockets;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Numerics;
//using System.Data;
//using System.Drawing;

namespace ConsoleApp3
{
    class Program
    {
        static void swap(ref int a,ref int b)
        {
            int t = a;
            a = b;
            b = t;
        }
        static int bfs(List<int>[] a, int od1, int n, ref int[] c)
        {
            Queue<int> q = new Queue<int>();
            q.Enqueue(od1);
            int br = 1;
            while (q.Count > 0)
            {
                foreach (int x in a[q.Peek()])
                {
                    if (!q.Contains(x) && c[x] == 0 && x!=od1)
                    {
                        q.Enqueue(x);
                        c[x] = c[q.Peek()] + 1;
                    }
                }
                q.Dequeue();
            }
            return 0;
        }
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            var a1 = Console.ReadLine().Split().Select(int.Parse).ToArray();
            List<int>[] a = new List<int>[n];
            for (int i = 0; i < n; i++)
                a[i] = new List<int>();
            for(int i=0;i<n-1;i++)
            {
                var s = Console.ReadLine().Split();
                int od = int.Parse(s[0]);
                int doo = int.Parse(s[1]);
                od--;doo--;
                a[od].Add(doo);
                a[doo].Add(od);
            }
            Dictionary<string, int> d = new Dictionary<string, int>();
            for(int i=0;i<n;i++)
            {
                int[] c = new int[n];
                bfs(a, i, n, ref c);
                for(int j=0;j<c.Length;j++)
                {
                    if(j!=i)
                    {
                        d[i + "|" + j] = c[j];
                    }
                }    
            }
            List<int> res = new List<int>();
            int q = int.Parse(Console.ReadLine());
            for(int i=0;i<q;i++)
            {
                var s = Console.ReadLine().Split();
                int id = int.Parse(s[0]);
                if(id==1)
                {
                    int l = int.Parse(s[1]);
                    int r = int.Parse(s[2]);
                    swap(ref a1[l - 1], ref a1[r - 1]);
                }
                else
                {
                    int l = int.Parse(s[1]);
                    int r = int.Parse(s[2]);
                    l--;r--;
                    int x = int.Parse(s[3]);
                    x--;
                    int max = int.MinValue;
                    for(int j=l;j<=r;j++)
                    {
                        if(d.ContainsKey(x + "|" + (a1[j]-1)))
                        max = Math.Max(max, d[x + "|" + (a1[j]-1)]);
                    }
                    res.Add(max);
                }
            
            }
            for(int i=0; i<res.Count;i++)
                Console.WriteLine(res[i]);
        }
    }
}