#include <cstdio>
#include <vector>
#include <algorithm>
#define maxn 100000

std :: vector <int> S[maxn + 10]; 
int n, q, u, v, mytimer, l, r, k;
int lca[5005][5005], dep[maxn + 10], f[maxn + 10], vis[maxn + 10], siz[maxn + 10], hson[maxn + 10], h[maxn + 10], dfn[maxn + 10];
int mindep[5005][5005], minline[5005][5005], dq[5005], mhead, mtail;

inline void dfs1(int i, int d) {
	dep[i] = d; vis[i] = siz[i] = 1;
	for (int j : S[i]) {
		if (vis[j]) {
			f[i] = j; continue;
		}
		dfs1(j, d + 1);
		siz[i] += siz[j];
		if (siz[j] > siz[hson[i]]) hson[i] = j;
	}
	return;
}
inline void dfs2(int i, int fa) {
	dfn[i] = ++mytimer; h[i] = fa;
	if (hson[i]) dfs2(hson[i], fa);
	for (int j : S[i])
		if (j != f[i] && j != hson[i]) dfs2(j, j);
	return;
}

int main(void) {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i < n; i++)
		scanf("%d %d", &u, &v), S[u].push_back(v), S[v].push_back(u);
	dfs1(1, 1);
	dfs2(1, 1);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
			int ii = i, jj = j;
			while (h[ii] != h[jj]) {
				if (dep[h[ii]] > dep[h[jj]]) ii = f[h[ii]];
				else jj = f[h[jj]];
			}
			lca[i][j] = lca[j][i] = dep[ii] < dep[jj] ? ii : jj;
		}
//	for (int i = 1; i <= n; i++, putchar('\n'))
//		for (int j = 1; j <= n; j++)
//			printf("%d ", lca[i][j]);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			lca[i][j] = dep[lca[i][j]];
	scanf("%d", &q);
	while (q--) {
		scanf("%d %d %d", &l, &r, &k); k--;
		int ans = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				mindep[i][j] = minline[i][j] = 2100114514;
		for (int i = 1; i <= n; i++) {
			mhead = 1, mtail = 0;
			for (int j = 1; j < k; j++) {
				while (mhead <= mtail && lca[i][j] <= lca[i][dq[mtail]]) mtail--;
				dq[++mtail] = j;
			}
			for (int j = k; j <= n; j++) {
				while (mhead <= mtail && dq[mhead] + k < j) mhead++;
				while (mhead <= mtail && lca[i][j] <= lca[i][dq[mtail]]) mtail--;
				dq[++mtail] = j;
				minline[i][j] = lca[i][dq[mhead]];
			}
		}
		for (int j = k; j <= n; j++) {
			mhead = 1, mtail = 0;
			for (int i = 1; i < k; i++) {
				while (mhead <= mtail && minline[i][j] <= minline[dq[mtail]][j]) mtail--;
				dq[++mtail] = i;
			}
			for (int i = k; i <= n; i++) {
				while (mhead <= mtail && dq[mhead] + k < i) mhead++;
				while (mhead <= mtail && minline[i][j] <= minline[dq[mtail]][j]) mtail--;
				dq[++mtail] = i;
				mindep[i][j] = minline[dq[mhead]][j];
			}
		}
		for (int i = l + k; i <= r; i++)
			ans = std :: max(ans, mindep[i][i]);
		printf("%d\n", ans);
	}
	
	return 0;
}
