#include <bits/stdc++.h>
using namespace std;
#define N 5005
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int n,m,LCA[N][N],ji[N][N];
int fa[N][15],dep[N];
vector <int> d[N];
void dfs(int x,int faa){
	dep[x]=dep[faa]+1;
	fa[x][0]=faa;
	for(int i=1;i<=13;i++){
		fa[x][i]=fa[fa[x][i-1]][i-1];
	}
	for(int y:d[x]){
		if(y==faa) continue;
		dfs(y,x);
	}
}
int lca(int x,int y){
	if(x==y) return x;
	if(dep[x]<dep[y]) swap(x,y);
//	printf("    %d %d %d %d\n",x,y,dep[x],dep[y]);
	if(~ji[x][y]) return ji[x][y];
	int jix=x,jiy=y;
	for(int i=13;i>=0;i--){
		if(dep[fa[x][i]]>=dep[y]) x=fa[x][i];
	}
	if(x==y) return ji[jix][jiy]=x;
	for(int i=13;i>=0;i--){
		if(fa[x][i]!=fa[y][i]){
			x=fa[x][i],y=fa[y][i];
		}
	}
	return ji[jix][jiy]=fa[x][0];
}
signed main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	memset(ji,-1,sizeof(ji));
	n=read();
	for(int i=1;i<n;i++){
		int x=read(),y=read();
		d[x].push_back(y);
		d[y].push_back(x);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++){
		LCA[i][i]=i;
		for(int j=i+1;j<=n;j++){
			LCA[i][j]=lca(LCA[i][j-1],j);
//			printf("%d %d %d\n",i,j,LCA[i][j]);
		}
	}
	m=read();
	while(m--){
		int l=read(),r=read(),k=read();
		int ans=0;
		for(int i=l;i<=r-k+1;i++){
			ans=max(ans,dep[LCA[i][i+k-1]]);
		}
		printf("%d\n",ans);
	}
	return 0;
}

