#include<bits/stdc++.h>
using namespace std;
#define N 5005
int n,l,k,r,x,y,m;
int dep[N],lc[N][N],Lc[N][N];
vector<int> G[N],S[N];
void dfs(int x,int fa){
	S[x].push_back(x);dep[x]=dep[fa]+1;lc[x][x]=x;
	for(int y:G[x]){
		if(y==fa) continue;
		dfs(y,x);
		for(int a:S[x]){
			for(int b:S[y]) lc[a][b]=lc[b][a]=x;
		}
		for(int b:S[y]) S[x].push_back(b);
	}
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		scanf("%d%d",&x,&y);
		G[x].push_back(y),G[y].push_back(x);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++){
		Lc[i][1]=i;
		for(int j=2;i+j-1<=n;j++) Lc[i][j]=lc[Lc[i][j-1]][i+j-1];
	}
	scanf("%d",&m);
	while(m--){
		scanf("%d%d%d",&l,&r,&k);int ans=0;
		for(int i=l;i+k-1<=r;i++) ans=max(ans,dep[Lc[i][k]]);
		printf("%d\n",ans);
	}
	return 0;
}

