#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,m;
int dep[N];
int h[2*N],nxt[2*N],pre[N],idx;
void add(int x,int y)
{
	h[++idx]=y;
	nxt[idx]=pre[x];
	pre[x]=idx;
}
void dfs(int x,int fa)
{
	for(int i=pre[x];i;i=nxt[i])
	{
		if(h[i]==fa) continue;
		dep[h[i]]=dep[x]+1;
		dfs(h[i],x);
	}
}
int dp(int x,int y,int k)
{
	int l=0,r=0;
	int q[N];
	int i,ans=0;
	for(i=x;i<=y;i++)
	{
		if(l!=r) while(q[l]>=i-k) l++;
		while(l!=r&&dep[q[r]]>dep[i]) r--;
		q[r++]=i;
		//cout<<l<<" "<<r<<" "<<q[l]<<endl;
		if(i>=x+k-1) ans=max(ans,dep[q[l]]); 
	}
	return ans;
}
int main()
{
	int i,j,k,l,r;
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for(i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y),add(y,x);
	}
	dep[1]=1;
	dfs(1,0);
	scanf("%d",&m);
	//for(i=1;i<=n;i++) cout<<dep[i]<<" ";
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&l,&r,&k);
		printf("%d\n",dp(l,r,k));
	}
}
