#include <bits/stdc++.h>

using namespace std;

class node
{
public:
    vector<int> v;
};

vector<vector<int>> dinamika;

int udaljenost(int x, int p, node stablo[], int pre)
{
    if (dinamika[x][p] != -1)
    {
        return dinamika[x][p];
    }
    if (stablo[p].v.size() == 1 && pre != -1)
    {
        dinamika[stablo[p].v[0]][x] = 0;
        dinamika[x][stablo[p].v[0]] = 0;
        return 0;
    }

    int br;

    for (int i = 0; i < stablo[p].v.size(); i++)
    {
        if (stablo[p].v[i] == x)
        {
            dinamika[x][p] = 1;
            dinamika[p][x] = 1;
            return 1;
        }
        else if (stablo[p].v[i] != pre)
        {
            br = udaljenost(x, stablo[p].v[i], stablo, p);
            dinamika[p][stablo[p].v[i]] = br;
            dinamika[stablo[p].v[i]][p] = br;
            if (br != 0)
            {
                dinamika[x][stablo[p].v[i]] = br;
                dinamika[stablo[p].v[i]][x] = br;
                return br + 1;
            }
        }
    }
    dinamika[p][x] = 0;
    dinamika[x][p] = 0;
    return 0;
}

int main()
{
    int n;
    cin >> n;
    int p[n];
    vector<int> red;
    for (int i = 0; i < n; i++)
    {
        red.push_back(-1);
    }
    for (int i = 0; i < n; i++)
    {
        dinamika.push_back(red);
    }

    for (int i = 0; i < n; i++)
    {
        cin >> p[i];
    }
    node stablo[n];
    int v1, v2;
    for (int i = 0; i < n - 1; i++)
    {
        cin >> v1 >> v2;
        stablo[v1 - 1].v.push_back(v2 - 1);
        stablo[v2 - 1].v.push_back(v1 - 1);
    }
    int Q;
    cin >> Q;
    vector<int> resenje;
    for (int i = 0; i < Q; i++)
    {
        int o;
        cin >> o;
        if (o == 1)
        {
            int a;
            cin >> a;
            int b;
            cin >> b;
            swap(p[a - 1], p[b - 1]);
        }
        else
        {
            int l, r, x;
            cin >> l >> r >> x;
            int rem = -1;
            int re;
            for (int j = l; j <= r; j++)
            {
                re = udaljenost(x - 1, p[j - 1] - 1, stablo, -1);
                if (re > rem)
                {
                    rem = re;
                }
            }
            resenje.push_back(rem);
        }
    }
    for (int i = 0; i < resenje.size(); i++)
    {
        cout << resenje[i] << endl;
    }
}