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

struct Node {
	//int name;
	//int id;
	Node* parentNode = nullptr;
	unordered_set<Node*> children;

	//Node(int oid, int oname, Node* oroot)
	//	: name(oname), id(oid), parentNode(oroot) {

	Node(Node * oroot)
		: parentNode(oroot) {

	}
};



int	findDistanceFromNodeToNode(Node* currentNode, Node* target, Node* from, unordered_map<Node*, int>& mapv) {
	if (mapv[currentNode] != 0) {
		return mapv[currentNode];
	}
	if (currentNode == target) {
		mapv[currentNode] = 1;
		return 1;
	}
	for (auto* a : currentNode->children) {
		if (a != from) {
			int b = findDistanceFromNodeToNode(a, target, currentNode, mapv);
			if (b != 0) {
				return b + 1;
			}
		}
	}
	if (currentNode->parentNode != from) {
		int b = findDistanceFromNodeToNode(currentNode->parentNode, target, currentNode, mapv);
		if (b != 0) {
			return b + 1;
		}
	}
	return 0;
}


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

	Node* root;
	root = new Node(nullptr);

	unordered_map<int, Node*> mapname;
	unordered_map<int, Node*> mapid;
	mapname[vec[1]] = root;
	mapid[1] = root;


	for (int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		Node* nodea = mapid[a];

		Node* newnode = new Node(nodea);
		nodea->children.insert(newnode);
		mapname[vec[b]] = newnode;
		mapid[b] = newnode;
	}



	int q = 0, t, ia, ib, l, r, x;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> t;
		if (t == 1) {
			cin >> ia >> ib;

			Node* nodea = mapname[ia];
			mapname[ia] = mapname[ib];
			mapname[ib] = nodea;
		}
		else if (t == 2) {
			cin >> l >> r >> x;
			int maxi = 0;
			Node* startingNode = mapname[x];
			unordered_map<Node*, int> mapv;

			for (int i = l; i <= r; i++)
				maxi = max(maxi, findDistanceFromNodeToNode(mapname[i], startingNode, nullptr, mapv) - 1);
			cout << maxi << endl;
		}

	}
	//print(root);


	return 0;
}