#include<bits/stdc++.h>

using namespace std;
const int N=5*1e5+100,M=20;
int dfn[N],dep[N];
int fa[N][M];
int loog[N];
int maxf[N][M];
vector<int> g[N];
int que[N][2];
int l[2],r[2];
int n,cnt,q;
struct quest{
	int L;
	int R,k;
	int num;
}s[N];
int ans[N];
bool cmp(quest a,quest b)
{
	return a.k<b.k;
}
void dfs(int u,int f)
{
//	cout<<cnt<<endl;
	dfn[u]=++cnt;
	dep[u]=dep[f]+1;
	fa[u][0]=f;
	for( int i=1 ; i<M ; i++ )
	{
		fa[u][i]=fa[fa[u][i-1]][i-1];
//		cout<<fa[u][i]<<" ";
	}
//	cout<<endl;
	for( int v:g[u] )if( !dfn[v] )dfs(v,u);
	return ;
}
int lca(int u,int v)
{
	if( dep[u]<dep[v] )swap(u,v);
	for( int i=M-1 ; i>=0 ; i-- )if( dep[fa[u][i]]>=dep[v] )u=fa[u][i];
	if( u==v )return dep[v];
	for( int i=M-1 ; i>=0 ; i-- )if( fa[u][i]!=fa[v][i] )u=fa[u][i],v=fa[v][i];
	return dep[fa[u][0]];
}
void cal(int x)
{
	l[0]=1;l[1]=1;
	r[0]=0;r[1]=0;
	loog[1]=0;
	for( int i=2 ; i<=n ; i++ )loog[i]=loog[i/2]+1;
	for( int i=1 ; i<=n ; i++ )
	{
		while( l[0]<=r[0] && i-que[l[0]][0]>=x )l[0]++;
		while( l[0]<=r[0] && dfn[que[r[0]][0]]<dfn[i] )r[0]--;
		que[++r[0]][0]=i;
		while( l[1]<=r[1] && i-que[l[1]][1]>=x )l[1]++;
		while( l[1]<=r[1] && dfn[que[r[1]][1]]>dfn[i] )r[1]--;
		que[++r[1]][1]=i;
		if( i<x )continue;
		maxf[i][0]=lca(que[l[0]][0],que[l[1]][1]);
//		cout<<i<<" "<<x<<" "<<que[l[0]][0]<<" "<<que[l[1]][1]<<endl;
	}
	for( int j=1 ; j<M ; j++ )
	{
		for( int i=1 ; i+(1<<j)-1<=n ; i++ )
		{
			maxf[i][j]=max(maxf[i][j-1],maxf[i+(1<<(j-1))][j-1]);
//		
		}
	}
}
int maxdep(int l,int r)
{
	int hlen=loog[r-l+1];
	return max(maxf[l][hlen],maxf[r-(1<<hlen)+1][hlen]);
}
int main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	int u,v;
	for( int i=1 ; i<n ; i++ )
	{
		scanf("%d%d",&u,&v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	scanf("%d",&q);
	for( int i=1 ; i<=q ; i++ )
	{
		scanf("%d%d%d",&s[i].L,&s[i].R,&s[i].k);
		s[i].num=i;
	}
	sort(s+1,s+1+q,cmp);
	dfs(1,1);
//	cout<<"ufdugfeuf"<<endl;
	int tmp;
	for( int i=1 ; i<=q ; i++ )
	{
		if( s[i].k!=s[i-1].k )cal(s[i].k);
		tmp=s[i].k;
//		cout<<i<<endl;
		while( s[i].k==tmp )
		{
			ans[s[i].num]=maxdep(s[i].L+s[i].k-1,s[i].R);
			i++;
		}
		i--;
	}
	for( int i=1 ; i<=q ; i++ )cout<<ans[i]<<endl;
	return 0;
}
