using System;
using System.Collections.Generic;

namespace kvalifikacije1._5
{
    class Grad
    {
        public int rb;
        public List<int> putevi = new List<int>();
        public int brp = 0;
        public Grad(int rb)
        {
            this.rb = rb;
        }
    }
    class Program
    {
        static void maxLength(int[] maxDuzina, Grad[] gradovi, int koren, int duzina, bool[] visited)
        {
            visited[koren - 1] = true;
            maxDuzina[koren - 1] = duzina;
            for(int i = 0; i < gradovi[koren - 1].brp; i++)
            {
                if (!visited[gradovi[koren - 1].putevi[i]-1]) maxLength(maxDuzina, gradovi, gradovi[koren - 1].putevi[i], duzina + 1, visited);
            }
        }
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            Grad[] gradovi = new Grad[n];
            int[] redosledGradova = new int[n];
            string[] str = Console.ReadLine().Split();
            for(int i = 0; i < n; i++)
            {
                gradovi[i] = new Grad(i + 1);
                redosledGradova[i] = int.Parse(str[i]);
            }
            for(int i = 0; i < n - 1; i++)
            {
                str = Console.ReadLine().Split();
                int a = int.Parse(str[0]);
                int b = int.Parse(str[1]);
                gradovi[a - 1].putevi.Add(b);
                gradovi[a - 1].brp++;
                gradovi[b - 1].putevi.Add(a);
                gradovi[b - 1].brp++;
            }
            int q = int.Parse(Console.ReadLine());
            int[] maxDuzina = new int[n];
            bool[] visited = new bool[n];
            int koren = 0;
            for(int i = 0; i < q; i++)
            {
                str = Console.ReadLine().Split();
                if (str[0] == "2")
                {
                    int l = int.Parse(str[1]);
                    int r = int.Parse(str[2]);
                    if (koren != int.Parse(str[3]))
                    {
                        koren = int.Parse(str[3]);
                        for (int j = 0; j < n; j++) visited[j] = false;
                        maxLength(maxDuzina, gradovi, koren, 0, visited);
                    }
                    int max = maxDuzina[redosledGradova[l-1]-1];
                    for (; l < r; l++)
                    {
                        if (maxDuzina[redosledGradova[l] - 1] > max) max = maxDuzina[redosledGradova[l]-1];
                    }
                    Console.WriteLine(max);
                }
                else
                {
                    int x = int.Parse(str[1]) - 1;
                    int y = int.Parse(str[2]) - 1;
                    int z = redosledGradova[x];
                    redosledGradova[x] = redosledGradova[y];
                    redosledGradova[y] = z;
                }
            }
        }
    }
}