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

const int MAXN = 5000 + 5;
const int T = 15;
int n, q;
vector<int> g[MAXN];
int dep[MAXN], fa[MAXN][T];
int p[MAXN][MAXN];
int d[MAXN][MAXN];

void dfs(int x, int f) {
	dep[x] = dep[f] + 1, fa[x][0] = f;
	for (int y : g[x]) {
		if (y == f) continue;
		dfs(y, x);
	}
}

int lca(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	for (int t = T - 1; t >= 0; t--)
		if (dep[fa[x][t]] >= dep[y])
			x = fa[x][t];
	for (int t = T - 1; t >= 0; t--)
		if (fa[x][t] != fa[y][t])
			x = fa[x][t], y = fa[y][t];
	if (x != y) x = fa[x][0], y = fa[y][0];
	return x;
}

int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i < n; i++) {
		int u, v; scanf("%d%d", &u, &v);
		g[u].push_back(v), g[v].push_back(u);
	}
	dfs(1, 0);
	for (int t = 1; t < T; t++)
		for (int x = 1; x <= n; x++)
			fa[x][t] = fa[fa[x][t - 1]][t - 1];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			p[i][j] = lca(i, j);
	for (int i = 1; i <= n; i++) {
		d[i][i] = i;
		for (int j = i + 1; j <= n; j++) {
			d[i][j] = p[d[i][j - 1]][j];
		}
	}
	scanf("%d", &q);
	for (int i = 1; i <= q; i++) {
		int l, r, k; scanf("%d%d%d", &l, &r, &k);
		int ans = 0;
		for (int j = l; j + k - 1 <= r; j++) ans = max(ans, dep[d[j][j + k - 1]]);
		printf("%d\n", ans);
	}
	return 0;
}

