#include <iostream>
#include <cstring>
using namespace std;
const int N = 500010, M = N << 1;
int h[N], e[M], ne[M], idx;
int dep[N], fa[N][25];
int lca[5010][5010], deep[5010][5010];
int g[25][N];
int mxdep;
void add(int a, int b){
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void dfs(int u, int fat){
	fa[u][0] = fat;
	for (int i = 1; i <= 23; i ++ ) fa[u][i] = fa[fa[u][i - 1]][i - 1];
	dep[u] = dep[fat] + 1;
	mxdep = max(mxdep, dep[u]);
	for (int i = h[u]; ~i; i = ne[i]){
		int j = e[i];
		if(j != fat) dfs(j, u);
	}
}
int LCA(int x, int y){
	if(dep[x] < dep[y]) swap(x, y);
	for (int i = 23; ~i; i -- )
		if(dep[fa[x][i]] >= dep[y])
			x = fa[x][i];
	if(x == y) return x;
	for (int i = 23; ~i; i -- )
		if(fa[x][i] != fa[y][i])
			x = fa[x][i], y = fa[y][i];
	return fa[x][0];
}
int main(){
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	memset(h, -1, sizeof h);
	int n;
	scanf("%d", &n);
	for (int i = 1; i < n; i ++ ){
		int a, b;
		scanf("%d%d", &a, &b);
		add(a, b);
		add(b, a);
	}
	dfs(1, 0);
	if(n <= 5000){
		for (int i = 1; i <= n; i ++ ){
			lca[i][i] = i; deep[i][i] = dep[i];
			for (int j = i + 1; j <= n; j ++ ){
				lca[i][j] = LCA(lca[i][j - 1], j);
				deep[i][j] = dep[lca[i][j]];
			}
		}
		int q;
		scanf("%d", &q);
		while (q -- ){
			int l, r, k;
			scanf("%d%d%d", &l, &r, &k);
			int res = 0;
			for (int i = l; i + k - 1 <= r; i ++ )
				res = max(res, deep[i][i + k - 1]);
			cout << res << '\n';
		}
	}else if(mxdep == n){
	}
}
