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

int n, q, anc[500005][20], st[20][500005], st2[20][500005], dep[500005], a[500005], mxd, anss[500005], lg[500005];
vector<int> g[500005];
struct qry {
	int l, r, k, id;
} e[500005];

void dfs(int u, int f) {
	anc[u][0] = f; dep[u] = dep[f] + 1; mxd = max(mxd, dep[u]);
	for (int i = 1; i <= 18; i++) anc[u][i] = anc[anc[u][i - 1]][i - 1];
	for (int v : g[u]) if (v != f) dfs(v, u);
}

int lca(int x, int y) {
	int xx = x, yy = y;
	if (dep[xx] < dep[yy]) swap(xx, yy);
	for (int i = 18; ~i; i--) if (dep[xx] - dep[yy] >= (1 << i)) xx = anc[xx][i];
	if (xx == yy) return xx;
	for (int i = 18; ~i; i--) if (anc[xx][i] != anc[yy][i]) xx = anc[xx][i], yy = anc[yy][i];
	return anc[xx][0];
}

struct node {
	int l, r, len, mx; 
};
node merge(node x, node y) {
	return (node){ x.l + (x.l == x.len ? y.l : 0), y.r + (y.r == y.len ? x.r : 0), x.len + y.len, max({x.mx, y.mx, x.r + y.l})};
}
#define lson (id << 1)
#define rson (id << 1 | 1)
struct sgt {
	node tr[400005];
	void build(int l, int r, int id) {
		tr[id] = {0, 0, r - l + 1, 0};
		if (l == r) return;
		int mid = (l + r) >> 1;
		build(l, mid, lson), build(mid + 1, r, rson);
	}
	void change(int l, int r, int id, int x, int val) {
		if (l == r) { tr[id] = {val, val, 1, val}; return; }
		int mid = (l + r) >> 1;
		if (x <= mid) change(l, mid, lson, x, val);
		else change(mid + 1, r, rson, x, val);
		tr[id] = merge(tr[lson], tr[rson]);
	}
	node query(int l, int r, int id, int x, int y) {
		if (l >= x && r <= y) return tr[id];
		int mid = (l + r) >> 1;
		if (y <= mid) return query(l, mid, lson, x, y);
		else if (x > mid) return query(mid + 1, r, rson, x, y);
		return merge(query(l, mid, lson, x, y), query(mid + 1, r, rson, x, y));
	}
} t;

void build() {
	for (int i = 1; i <= n; i++) st[0][i] = dep[i];
	for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
	for (int i = 1; i <= 18; i++) {
		for (int j = 1; j <= n - (1 << i) + 1; j++) {
			st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
		}
	}
}

int qmx(int l, int r) {
	int gc = lg[r - l + 1];
	return max(st[gc][l], st[gc][r - (1 << gc) + 1]);
}
int qmn(int l, int r) {
	int gc = lg[r - l + 1];
	return min(st[gc][l], st[gc][r - (1 << gc) + 1]);
}

vector<int> poss[100005];
void solve(int l, int r, vector<qry> qs) {
	if (!qs.size()) return;
	if (l == r) { for (auto v : qs) anss[v.id] = l; return; }
	int mid = (l + r) >> 1;
	vector<qry> ql, qr;
	for (int i = mid + 1; i <= r; i++) {
		for (int j : poss[i]) t.change(1, n, 1, j, 1);
	}
	for (auto v : qs) {
		if (t.query(1, n, 1, v.l, v.r - 1).mx >= v.k - 1) qr.push_back(v);
		else ql.push_back(v);
	}
	for (int i = mid + 1; i <= r; i++) {
		for (int j : poss[i]) t.change(1, n, 1, j, 0);
	}
	solve(mid + 1, r, qr);
	for (int i = mid + 1; i <= r; i++) {
		for (int j : poss[i]) t.change(1, n, 1, j, 1);
	}
	solve(l, mid, ql);
}

void Sub1() {
	dfs(1, 0);
	for (int i = 1; i < n; i++) a[i] = dep[lca(i + 1, i)], poss[a[i]].push_back(i);
//	for (int i = 1; i < n; i++) printf("[%d]\n", a[i]);
	build();
	scanf("%d", &q);
	for (int i = 1; i <= q; i++) scanf("%d %d %d", &e[i].l, &e[i].r, &e[i].k), e[i].id = i;
	vector<qry> al;
	for (int i = 1; i <= q; i++) {
		if (e[i].k == 1) anss[e[i].id] = qmx(e[i].l, e[i].r);
		else al.push_back(e[i]);
	}
	t.build(1, n - 1, 1);
	solve(1, mxd, al);
	for (int i = 1; i <= q; i++) printf("%d\n", anss[i]);
}

void Sub2() {
	dfs(1, 0); for (int i = 1; i < n; i++) a[i] = dep[lca(i + 1, i)];
	for (int i = 1; i < n; i++) st[0][i] = a[i];
	for (int i = 2; i < n; i++) lg[i] = lg[i >> 1] + 1;
	for (int i = 1; i <= 18; i++) {
		for (int j = 1; j < n - (1 << i) + 1; j++) {
			st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
		}
	}
	scanf("%d", &q);
	for (int i = 1, l, r, k; i <= q; i++) {
		scanf("%d %d %d", &l, &r, &k);
		if (k == 1) printf("%d\n", dep[l]);
		else printf("%d\n", qmn(l, r - 1));
	}
}

int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1, u, v; i < n; i++) {
		scanf("%d %d", &u, &v);
		g[u].push_back(v); g[v].push_back(u);
	}
	if (n <= 100000 && q <= 100000) Sub1();
	else Sub2();
}
