#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define bll __int128
#define vii vector<int>
#define vll vector<ll>
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define sec second
#define mp make_pair
#define qii queue<int>
#define puu pair<ull,ull>
#define clr clear
#define re register
using namespace std;
const int N=5e5+5,M=1e6+5,K=1e7+5,L=5005;
bool ST;
struct node{int l,r,ans;}tr[K],fz;
int head[N],nex[M],to[M],tot;
int deg[N],rt[N],ls[K],rs[K],cnt,fx,fy,las;
int n,q,dfntot;
int dep[N],numdep[N],dfn[N],mxdfn[N],rk[N],fa[N],siz[N],top[N],son[N];
short LCA[L][L],Ans[L][L];
int F[N][19],pow2[21],lg2[N];
void add_edge(int x,int y){deg[x]++,tot++,nex[tot]=head[x],head[x]=tot,to[tot]=y;}
inline node merge(node x,node y,int lenl,int lenr){return (node){x.l+(x.l==lenl)*y.l,y.r+(y.r==lenr)*x.r,max(max(x.ans,y.ans),x.r+y.l)};}
int add(int i,int l,int r)
{
	re int now=++cnt;
	tr[now]=tr[i],ls[now]=ls[i],rs[now]=rs[i];
	if(l==r){tr[now]=(node){1,1,1};return now;}
	re int mid=(l+r)>>1;
	if(fx<=mid)ls[now]=add(ls[i],l,mid);
	else rs[now]=add(rs[i],mid+1,r);
	tr[now]=merge(tr[ls[now]],tr[rs[now]],mid-l+1,r-mid);return now;
}
void ask(int i,int l,int r)
{
	if(fx>r||l>fy)return ;
	if(fx<=l&&r<=fy){if(fz.l==-1)fz=tr[i];else fz=merge(fz,tr[i],las,r-l+1);las=r-l+1;return ;}
	int mid=(l+r)>>1;
	ask(ls[i],l,mid),ask(rs[i],mid+1,r);
}
void dfs_line(int x,int f)
{
	dep[x]=dep[f]+1,numdep[dep[x]]=x;int son=0;
	for(int i=head[x];i;i=nex[i])
	{
		int y=to[i];
		if(y!=f)dfs_line(y,x),son=y;
	}
	fx=x,rt[x]=add(rt[son],1,n);
}
bool ND;
void dfs(int x,int f)
{
	dfn[x]=++dfntot,rk[dfntot]=x,fa[x]=f,dep[x]=dep[f]+1;
	for(int i=head[x];i;i=nex[i])
	{
		int y=to[i];
		if(y!=f)dfs(y,x);
	}
	mxdfn[x]=dfntot;
}
void dfs1(int x,int f)
{
	siz[x]=1,fa[x]=f,dep[x]=dep[f]+1;
	for(int i=head[x];i;i=nex[i])
	{
		int y=to[i];
		if(y!=f)
		{
			dfs1(y,x);
			if(siz[son[x]]<siz[y])son[x]=y;
		}
	}
}
void dfs2(int x,int tp)
{
	top[x]=tp;
	if(!son[x])return ;
	dfs2(son[x],tp);
	for(int i=head[x];i;i=nex[i])
	{
		int y=to[i];
		if(y!=fa[x]&&y!=son[x])dfs2(y,y);
	}
}
int lca(int x,int y){
	while(top[x]!=top[y])
	{
		if(dep[top[x]]>dep[top[y]])x=fa[top[x]];
		else y=fa[top[y]];
	}
	if(dep[x]<dep[y])return x;
	return y;
}
int Query(int l,int r){int k=lg2[r-l+1];return lca(F[l][k],F[r-pow2[k]+1][k]);}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	pow2[0]=1;for(int i=1;i<=20;i++)pow2[i]=pow2[i-1]*2;
	for(int i=2;i<=5e5;i++)lg2[i]=lg2[i>>1]+1;
	cin>>n;
	for(int i=1,x,y;i<n;i++)cin>>x>>y,add_edge(x,y),add_edge(y,x);
	cin>>q;
	int BZ_line=1;
	for(int i=1;i<=n;i++)BZ_line&=(deg[i]<=2);
	if(BZ_line)
	{
		dfs_line(1,0);
		while(q--)
		{
			int l,r,k;cin>>l>>r>>k,fx=l,fy=r;
			int L=1,R=n-k+1,ans=0;
			while(L<=R)
			{
				int mid=(L+R)>>1;
				fz=(node){-1,-1,-1},ask(rt[numdep[mid]],1,n);
				if(fz.ans>=k)ans=mid,L=mid+1;
				else R=mid-1;
			}
			cout<<ans<<endl;
		}
		return 0;
	}
	if(n<=5000)
	{
		dfs(1,0);
		for(int i=1;i<=n;i++)LCA[i][i]=i;
		for(int i=2;i<=n;i++)
		{
			int nowl=dfn[i],nowr=mxdfn[i];
			int upl=dfn[fa[i]],upr=mxdfn[fa[i]];
			for(int j=nowl;j<=nowr;j++)
			{
				for(int k=upl;k<nowl;k++)LCA[rk[j]][rk[k]]=LCA[rk[k]][rk[j]]=fa[i];
				for(int k=nowr+1;k<=upr;k++)LCA[rk[j]][rk[k]]=LCA[rk[k]][rk[j]]=fa[i];
			}
		}
		for(int i=1;i<=n;i++)
		{
			Ans[i][i]=i;
			for(int j=i+1;j<=n;j++)Ans[i][j]=LCA[Ans[i][j-1]][j];
		}
		while(q--)
		{
			int l,r,k,ans=0;
			cin>>l>>r>>k;
			for(int i=l;i+k-1<=r;i++)ans=max(ans,dep[Ans[i][i+k-1]]);
			cout<<ans<<endl;
		}
		return 0;
	}
	dfs1(1,0),dfs2(1,1);
	for(int i=1;i<=n;i++)F[i][0]=i;
	for(int i=1;i<=18;i++)for(int j=1;j<=n;j++)F[j][i]=lca(F[j][i-1],F[min(j+pow2[i-1],n)][i-1]);
	while(q--)
	{
		int l,r,k;
		cin>>l>>r>>k;
		cout<<dep[Query(l,r)]<<endl;
	}
}
