#include <iostream>
//#include <fstream>
//#include <iomanip>
//#include <cmath>
#include <algorithm>
//#include <string>
#include <vector>
#include <list>
//#include <utility>
//#include <map>
//#include <ctime>
//#include <cctype>
//#include <cstdlib>
#include <limits>
//#include <numeric>
//#include <mycpp.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;

int n;
vi naselja;
vvi putevi;
vi resenja;


void Slucaj1();
int Slucaj2();
vi Daljine(int x, int l, int r);
vi Razdaljine(int poc);

int main()
{
	scanf("%d", &n);
	naselja.resize(n + 1);
	putevi.resize(n + 1);
	for(int i = 0; i < n; i++)
	{
		scanf("%d", &naselja[i]);
		naselja[i]--;
	}
	for(int i = 0; i < n - 1; i++)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		a--; b--;
		putevi[a].push_back(b);
		putevi[b].push_back(a);
	}
	int m;
	scanf("%d", &m);
	for(int i = 0; i < m; i++)
	{
		int sl;
		scanf("%d", &sl);
		if(sl == 1)
			Slucaj1();
		else
			resenja.push_back(Slucaj2());
	}
	for(int i = 0; i < resenja.size(); i++)
		cout << resenja[i] << endl;
}

void Slucaj1()
{
	int a, b;
	scanf("%d%d", &a, &b);
	a--; b--;
	int t = naselja[a];
	naselja[a] = naselja[b];
	naselja[b] = t;
}

int Slucaj2()
{
	int l, r, x;
	scanf("%d%d%d", &l, &r, &x);
	x--; l--; r--;
	vi razdaljine = Daljine(x, l, r);
	return *max_element(razdaljine.begin(), razdaljine.end());
}

vi Daljine(int x, int l, int r)
{
	vi raz = Razdaljine(x);
	vi vrati;
	for(int i = l; i <= r; i++)
		vrati.push_back(raz[naselja[i]]);
	return vrati;
}


vi Razdaljine(int poc)
{
	vi raz(n, numeric_limits<int>::max());
    list<int> red;
    vb posecen(n, false);
 
    posecen[poc] = true;
    raz[poc] = 0;
    red.push_back(poc);
 
    while (!red.empty()) 
	{
        int u = red.front();
        red.pop_front();
        for (int i = 0; i < putevi[u].size(); i++) 
		{
            if (!posecen[putevi[u][i]]) 
			{
                posecen[putevi[u][i]] = true;
                raz[putevi[u][i]] = raz[u] + 1;
                red.push_back(putevi[u][i]);
            }
        }
    }
    return raz;
}