#include<bits/stdc++.h>
using namespace std;
const int N=5e5+3;
int n,q,h[N],x,y,fa[N],sz[N],dep[N],l,r,k;
int tp[N],sn[N],lc[5003][15];
bool f;
struct edge{
	int to,nxt;
}e[N*2];
void dfs(int x){
	sz[x]=1;
	for(int i=h[x];i;i=e[i].nxt){
		int to=e[i].to;
		if(to==fa[x])continue;
		fa[to]=x,dep[to]=dep[x]+1;
		dfs(to);
		sz[x]+=sz[to];
		if(sz[to]>sz[sn[x]])sn[x]=to;
	}
}
void dfs(int x,int y){
	tp[x]=y;
	if(sn[x])dfs(sn[x],y);
	for(int i=h[x];i;i=e[i].nxt){
		int to=e[i].to;
		if(to==fa[x]||to==sn[x])continue;
		dfs(to,to);
	}
}
int lca(int x,int y){
	while(tp[x]!=tp[y]){
		if(dep[tp[x]]>dep[tp[y]])x=fa[tp[x]];
		else y=fa[tp[y]];
	}
	if(dep[x]<dep[y])return x;
	return y;
}
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);
		e[i*2]={y,h[x]},h[x]=i*2;
		e[i*2+1]={x,h[y]},h[y]=i*2+1;
	}
	fa[1]=dep[1]=1;
	dfs(1),dfs(1,1);
	for(int i=1;i<=n;i++)lc[i][0]=i;
	for(int i=1;i<14;i++){
		for(int j=1<<i;j<=n;j++){
			lc[j-(1<<i)+1][i]=lca(lc[j-(1<<i)+1][i-1],lc[j-(1<<i-1)+1][i-1]);
		}
	}
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d",&l,&r,&k);
		int t=log2(k);
		if(n>5000)cout<<dep[lca(lc[l][t],lc[r-(1<<t)+1][t])]<<'\n';
		else{
			int s=0;
			while(l+k<=r)s=max(s,dep[lca(lc[l][t],lc[l+k-(1<<t)][t])]),l++;
			cout<<s<<'\n';
		}
	}
}
