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

struct JZ {
	int to, nxt;
}edge[1000005];
struct Lars {
	int l, r, val;
}tr[2000005];
int n, q, cnt, ans;
int head[500005], f[500005][25], dep[500005], siz[500005], t[500005];
void add(int u, int v) {
	edge[++cnt] = {v, head[u]};
	head[u] = cnt;
}
void dfs(int x, int fa) {
	f[x][0] = fa, dep[x] = dep[fa]+1;
	for (int i = 1; i <= 20; i++) f[x][i] = f[f[x][i-1]][i-1];
	for (int i = head[x]; i; i = edge[i].nxt) {
		int v = edge[i].to;
		if (v == fa) continue;
		dfs(v, x);
	}
}
int lca(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	for (int i = 20; i >=  0; i--) {
		if (dep[f[x][i]] >= dep[y]) x = f[x][i];
	}
	if (x == y) return x;
	for (int i = 20; i >= 0; i--) {
		if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
	}
	return f[x][0];
}
void dfss(int u, int fa) {
	siz[u] = 1, t[1] = max(t[1], dep[u]);
	for (int i = head[u]; i; i = edge[i].nxt) {
	    int v = edge[i].to;
	    if (v == fa) continue;
		dfss(v, u);
	    siz[u] += siz[v];
	    t[siz[u]] = max(t[siz[u]], dep[u]);
	}
}
void build(int x, int l, int r) {
	tr[x].l = l, tr[x].r = r;
	if (l == r) {
		tr[x].val  = l;
		return ;
	}
	int mid = (l+r)/2;
	build(x*2, l, mid), build(x*2+1, mid+1, r);
	tr[x].val = lca(tr[x*2].val, tr[x*2+1].val);
}
int query(int x, int L, int R, int k) {
	if (L <= tr[x].l && tr[x].r <= R) return tr[x].val;
	int res1 = 0, res2 = 0, res, mid = (tr[x].l+tr[x].r)/2;
	if (L <= mid) res1 = query(x*2, L, R, k);
	if (mid < R) res2 = query(x*2+1, L, R, k);
	if (res1 && res2) res = lca(res1, res2);
	else if (res1) res = res1;
	else if (res2) res = res2;
	if ((tr[x].r-tr[x].l+1) >= k) ans = max(ans, dep[res]);
	return res;
}
int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		add(u, v), add(v, u);
	}
	dfs(1, 0);
	build(1, 1, n);
	dfss(1, 0);
	cin >> q;
	for (int i = 1, l, r, k; i <= q; i++) {
		cin >> l >> r >> k;
		if (l == 1 && r == n) cout << t[k] << '\n';
		else {
			ans = 0;
			int a = query(1, l, r, k);
			cout << max(ans, dep[a]) << '\n';
		}
	}
	return 0;
} 
