#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

vector<int> p;
int** routes;
map<int, vector<int> > unitroutes;
vector<int> response;

void swap(int a, int b) {
	int tmp = p[a];
	p[a] = p[b];
	p[b] = tmp;
}

int searchforpath_(int srcd, int source, int distance) {
	if (srcd == source)
		return distance;
	else {
		int i;
		for (i = 0; i < unitroutes[source].size(); i++) {
			if (source != unitroutes[source][i])
				if (unitroutes[source].size() != 1 && unitroutes[source][0] == source)
					return searchforpath_(srcd, unitroutes[source][i], distance + 1);
		}
	}
}

int searchforpath(int start, int trazen) {
	return searchforpath_(trazen, start, 1);
}


void parser(string& input) {
	if (input[0] == '1') {
		int i = 2;
		int extracted[2];
		char* processed[2];
		processed[0] = new char[256];
		processed[1] = new char[256];
		for (int j = 0; j < 2; j++) {
			while (input[i] != ' ' && input[i] != '\n') {
				processed[j][j - 2] = input[i];
				i++;
			}
			extracted[j] = atoi(processed[j]);
		}
		swap(extracted[0], extracted[1]);
	}
	else {
		int i = 2;
		int extracted[3];
		char* processed[3];
		for (int j = 0; j < 3; j++) {
			processed[j] = new char[256];
			while (input[i] != ' ' && input[i] != '\n') {
				processed[j][i - 2] = input[i];
				i++;
			}
			extracted[j] = atoi(processed[j]);
		}
		int max = 0;
		for (int i = p[extracted[0]]; i <= p[extracted[1]]; i++) {
			int res = searchforpath(p[extracted[2]], i);
			if (res > max)
				max = res;
		}
		response.push_back(max);
	}
}

int main() {
	int n; cin >> n;
	p.resize(n);
	int i;
	for (i = 0; i < n; i++)
		cin >> p[i];
	routes = new int* [n + 1];
	for (i = 0; i <= n; i++)
		routes[i] = (int*)calloc(n + 1, sizeof(int));
	for (i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		routes[a][b] = 1;
		routes[b][a] = 1;
		unitroutes[a].push_back(b);
		unitroutes[b].push_back(a);
	}
	int q; cin >> q;
	for (i = 0; i < q + 1; i++) {
		string input;
		getline(cin, input, '\n');
		if(i != 0)
			parser(input);
//
//		else if (extracted[0] == 2) {

//		}
	}
	for (i = 0; i < response.size(); i++)
		cout << response[i] << "\n";
	return 0;
}