#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int N=5e5+10;
int n,m,l,r,k,u,v,mi[20][N],f[20][N],lg2[N],dfn[N],idx,dep[N];
vector<int> e[N];
void dfs(int u,int fa){
	dep[u]=dep[fa]+1;
	dfn[u]=++idx;
	mi[0][dfn[u]]=fa;
	for(int v:e[u])if(v!=fa)dfs(v,u);
}
int get(int u,int v){return dfn[u]<dfn[v]?u:v;}
int LCA(int u,int v){
	if(u==v)return u;
	if((u=dfn[u])>(v=dfn[v]))swap(u,v);
	int k=lg2[v-u++];
	return get(mi[k][u],mi[k][v-(1<<k)+1]);
}
int ask(int l,int r){
	int k=lg2[r-l+1];
//	cout<<"ask: ["<<l<<","<<r<<"]: "<<
	return dep[LCA(f[k][l],f[k][r-(1<<k)+1])];
}
void init(){
	dfs(1,0);
	for(int i=1;i<=n;i++)lg2[i]=__lg(i),f[0][i]=i;
	for(int j=1;j<=lg2[n];j++)for(int i=1;i+(1<<j)-1<=n;i++)mi[j][i]=get(mi[j-1][i],mi[j-1][i+(1<<(j-1))]);
	for(int j=1;j<=lg2[n];j++)for(int i=1;i+(1<<j)-1<=n;i++)f[j][i]=LCA(f[j-1][i],f[j-1][i+(1<<(j-1))]);
}
int ask1(int l,int r,int k){
	int ans=0;
	for(int i=l;i+k-1<=r;i++)ans=max(ans,ask(i,i+k-1));
	return ans;
}
signed main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	init();
	cin>>m;
	while(m--){
		cin>>l>>r>>k;
		cout<<ask1(l,r,k)<<endl;
	}
}

