using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication131
{
    class Program
    {
        static void zameni_poz(int[] grd, int a, int b)
        {
            int t = grd[a];
            grd[a] = grd[b];
            grd[b] = t;
        }
        static int ras(List<int>[] put, int p, int k, ref bool[] posecen, ref int[,] rastojanje)
        {
            if (p == k) return 0;
            if (posecen[p]) return -100;
            if (put[p].Contains(k))
            {
                posecen[p] = true;
                return 1;
            }
            posecen[p] = true;
            int min;
            if (rastojanje[put[p][0], k] == 0)
            {
                min = ras(put, put[p][0], k, ref posecen, ref rastojanje) + 1;
                rastojanje[put[p][0], k] = min;
                rastojanje[k, put[p][0]] = min;
            }
            else
            {
                min = rastojanje[put[p][0], k];
            }
            for (int i = 1; i < put[p].Count - 1; i++)
            {
                int ss;
                if (rastojanje[put[p][i], k] == 0)
                {
                    ss = ras(put, put[p][i], k, ref posecen, ref rastojanje) + 1;
                    rastojanje[k, put[p][i]] = ss;
                    rastojanje[put[p][i], k] = ss;
                }
                else
                {
                    ss = rastojanje[put[p][i], k];
                }
                if (ss >= 0)
                {
                    if (ss < min)
                    {
                        min = ss;
                    }
                }
            }
            return min;
        }
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string[] str = Console.ReadLine().Split();
            int[] grd = new int[n + 1];
            grd[0] = 0;
            List<int>[] put = new List<int>[n + 1];
            put[0] = new List<int>();
            for (int i = 1; i <= n; i++)
            {
                grd[i] = int.Parse(str[i - 1]);
                put[i] = new List<int>();
            }
            for (int i = 0; i < n - 1; i++)
            {
                str = Console.ReadLine().Split();
                int a = int.Parse(str[0]);
                int b = int.Parse(str[1]);
                put[a].Add(b);
                put[b].Add(a);
            }
            int[,] rastojanje = new int[n + 1, n + 1];
            for (int i = 0; i < n + 1; i++)
            {
                for (int j = 0; j < n + 1; j++)
                {
                    rastojanje[i, j] = 0;
                }
            }
            int q = int.Parse(Console.ReadLine());
            for (int i = 0; i < q; i++)
            {
                string[] str1 = Console.ReadLine().Split();
                if (str1[0] == "1")
                {
                    zameni_poz(grd, int.Parse(str1[1]), int.Parse(str1[2]));
                }
                else
                {
                    int l = int.Parse(str1[1]);
                    int r = int.Parse(str1[2]);
                    int x = int.Parse(str1[3]);
                    bool[] posecen = new bool[n + 1];
                    int max;
                    if (rastojanje[x, grd[l]] == 0)
                    {
                        for (int j = 0; j < n + 1; j++)
                        {
                            posecen[j] = false;
                        }
                        int s = ras(put, x, grd[l], ref posecen, ref rastojanje);
                        max = s;
                        rastojanje[x, grd[l]] = s;
                        rastojanje[grd[l], x] = s;
                    }
                    else
                    {
                        max = rastojanje[x, grd[l]];
                    }
                    for (int j = l + 1; j <= r; j++)
                    {
                        if (rastojanje[x, grd[j]] == 0)
                        {
                            for (int h = 0; h < n + 1; h++)
                            {
                                posecen[h] = false;
                            }
                            int s = ras(put, x, grd[j], ref posecen, ref rastojanje);
                            max = Math.Max(max,s);
                            rastojanje[x, grd[j]] = s;
                            rastojanje[grd[j], x] = s;
                        }
                        else
                        {
                            max = Math.Max(max, rastojanje[x, grd[j]]);
                        }
                    }
                    Console.WriteLine(max);
                }
            }
            Console.ReadKey();
        }
    }
}