#include<bits/stdc++.h>
using namespace std;
int n,m,k,cnt,key[500010],x,y,fa[500010][22],st[500010][22],dep[500010],ans,ct;
int read()
{
	int r=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') w=ch=='-'?-1:w,ch=getchar();
	while(ch>='0'&&ch<='9') r=r*10+ch-'0',ch=getchar();
	return r*w;
}
struct edge
{
	int scd,nxt;
}a[1000010];
void add(int x,int y)
{
	a[++cnt]={y,key[x]};
	key[x]=cnt;
	a[++cnt]={x,key[y]};
	key[y]=cnt;
}
void dfs(int x)
{
	dep[x]=dep[fa[x][0]]+1;
	for(int i=1;i<=21;i++)
	fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=key[x];i;i=a[i].nxt)
	if(a[i].scd!=fa[x][0])
	fa[a[i].scd][0]=x,dfs(a[i].scd);
}
int lca(int x,int y)
{
	if(dep[x]<dep[y])
	swap(x,y);
	while(dep[x]>dep[y])
	x=fa[x][int(log2(dep[x]-dep[y]))];
	if(x==y)
	return x;
	for(int i=21;i>=0;i--)
	if(fa[x][i]!=fa[y][i])
	x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
int ask(int l,int r)
{
	int k=log2(r-l+1);
	return lca(st[l][k],st[r-(1<<k)+1][k]);
}
int main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	for(int i=1;i<n;i++)
	x=read(),y=read(),add(x,y);
	fa[1][0]=1;
	dfs(1);
	for(int i=1;i<n;i++)
	st[i][0]=lca(i,i+1);
	for(int i=1;i<=21;i++)
	for(int j=1;j+(1<<i)-1<n;j++)
	st[j][i]=lca(st[j][i-1],st[j+(1<<i-1)][i-1]);
	m=read();
	for(int i=1;i<=m;i++)
	{
		x=read(),y=read()-1,k=read()-1;
		ans=0;
		if(!k)
		{
			for(int l=x;l<=y+1;l++)
			ans=max(ans,dep[l]);
		}
		else
		for(int l=x;l<=y-k+1;l++)
		for(int r=max(l+k-1,l);r<=y;r++)
		ans=max(ans,dep[ask(l,r)]);
		printf("%d\n",ans);
	}
} 
