#include <bits/stdc++.h>
using namespace std;

void print(const vector<uint16_t>& vec) {
	for (const auto& a : vec) {
		cout << a << " ";
	}
	cout << endl;
}

int izracunaj_razdaljinu(int i, int x, unordered_map <uint16_t, pair<uint16_t, vector<uint16_t>>>& mapi) {
	const auto& prvi = mapi[i];
	const auto& drugi = mapi[x];
	int minbroj = min(prvi.second.size(), drugi.second.size());
	//cout << "-------- " << i << " " << x << endl;
	//print(prvi.second);
	//print(drugi.second);
	int poklapasedo = 0;
	for (int i = 0; i < minbroj; i++) {
		if (prvi.second[i] == drugi.second[i]) {
			poklapasedo = i;
		}
	}
	return (prvi.first + drugi.first) - (2 * poklapasedo);
}


int main(int argc, char const *argv[])
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int n;
	cin >> n;
	vector<uint16_t> vec(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> vec[i];
	}

	unordered_map<uint16_t, pair<uint16_t, vector<uint16_t>>> mapi;
	unordered_map<uint16_t, uint16_t> nametoid;

	mapi[1] = {0, {1}};
	nametoid[vec[1]] = 1;

	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		mapi[v] = {mapi[u].first + 1, mapi[u].second};
		mapi[v].second.push_back(v);
		//cout << "---   " << u << " " << v << endl;
		//print(mapi[v].second);

		nametoid[vec[v]] = v;
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int t;
		cin >> t;
		if (t == 1) {
			int a, b;
			cin >> a >> b;

			int namea = nametoid[a];
			nametoid[a] = nametoid[b];
			nametoid[b] = namea;
		}
		else if (t == 2) {
			int l, r, x;
			cin >> l >> r >> x;

			int maxi = -1;
			for (int i = l; i <= r; i++) {
				maxi = max(maxi, izracunaj_razdaljinu(nametoid[i], nametoid[x], mapi));
			}
			//cout << "========" << endl;
			cout << maxi << endl;
			//cout << "++++++++" << endl;
		}
	}



	return 0;
}