#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 3;
int n, q;
struct Edge {
	int to, nxt;
} e[maxn << 1];
int head[maxn], tot;
void add(int u, int v) {
	tot++;
	e[tot] = {v, head[u]};
	head[u] = tot;
}
int f[maxn], dep[maxn], siz[maxn], id[maxn], top[maxn], son[maxn], idx;
void dfs(int u, int fa) {
	f[u] = fa, dep[u] = dep[fa] + 1, siz[u] = 1;
	for (int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if (v == fa) continue;
		dfs(v, u);
		siz[u] += siz[v];
		if (siz[son[u]] < siz[v]) son[u] = v;
	}
}
void dfs2(int u, int tp) {
	top[u] = tp, id[u] = ++idx;
	if (son[u] == 0) return ;
	dfs2(son[u], tp);
	for (int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if (v == f[u] || v == son[u]) continue;
		dfs2(v, v);
	}
}
int lca(int u, int v) {
	while (top[u] != top[v]) {
		if (dep[top[u]] < dep[top[v]]) swap(u, v);
		u = f[top[u]];
	}
	return dep[u] < dep[v] ? u : v;
}
int st[maxn][21], qlog[maxn];
void init() {
	qlog[0] = -1;
	for (int i = 1; i <= n; i++) qlog[i] = qlog[i >> 1] + 1, st[i][0] = i;
	for (int j = 1; j <= 20; j++) {
		for (int i = 1; i + (1 << j) - 1 <= n; i++) {
			st[i][j] = lca(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	cin >> n;
	for (int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		add(u, v), add(v, u);
	}
	dfs(1, 0);
	dfs2(1, 1);
	init();
	cin >> q;
	while (q--) {
		int x, y, k;
		cin >> x >> y >> k;
		int res = 0, s = qlog[k];
		for (int i = x; i + k - 1 <= y; i++) {
			 res = max(dep[lca(st[i][s], st[i + k - (1 << s)][s])], res);
		}
		cout << res << '\n';
	}
	return 0;
}
