#include<bits/stdc++.h>
#define pb push_back
#define P make_pair
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define fi first
#define se second
using namespace std;
typedef long long ll;
inline ll read(){
	ll x=0;char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
	return x;
}
const int N=5e5+3;
const int NN=1e7+3;
const int M=1e7+3;
int n,m,ans[N],S[N],sz[N],dep[N];
vector<int>G[N];
struct Node{int l,r,x;}b[NN];int ct;
int rt[N],tt,t[M],son[M][2];
int bin[M],ttt;
inline void dfs1(int u,int fa){
	dep[u]=dep[fa]+1,sz[u]=1;
	for(int v:G[u])if(v!=fa){
		dfs1(v,u);sz[u]+=sz[v];
		if(sz[v]>sz[S[u]])S[u]=v;
	}
}
#define mid ((l+r)>>1)
inline void del(int x){
	if(!x)return ;
	del(son[x][0]),del(son[x][1]);
	son[x][0]=son[x][1]=t[x]=0;
	bin[++ttt]=x;
}
inline void ad(int &x,int l,int r,int p){
	if(!x){
		if(ttt)x=bin[ttt--];
		else x=++tt;
	}
	++t[x];
	if(l==r)return ;
	if(p<=mid)ad(son[x][0],l,mid,p);
	else ad(son[x][1],mid+1,r,p);
}
inline int fd1(int x,int l,int r,int p){//left 0
	if(l>p||t[x]==r-l+1)return 0;
	if(l==r)return l;
	int res=fd1(son[x][1],mid+1,r,p);
	if(res)return res;
	return fd1(son[x][0],l,mid,p);
}
inline int fd2(int x,int l,int r,int p){//right 0
	if(r<p||t[x]==r-l+1)return n+1;
	if(l==r)return l;
	int res=fd2(son[x][0],l,mid,p);
	if(res<=n)return res;
	return fd2(son[x][1],mid+1,r,p);
}
inline int fd3(int x,int l,int r,int p){//right 1
	if(r<p||t[x]==0)return n+1;
	if(l==r)return l;
	int res=fd3(son[x][0],l,mid,p);
	if(res<=n)return res;
	return fd3(son[x][1],mid+1,r,p);
}
inline void ad(int x,int l,int r){
	int L=fd1(rt[x],1,n,l-1)+1,R=fd2(rt[x],1,n,r+1)-1;
	b[++ct]={L,R,dep[x]};
	rep(i,l,r)ad(rt[x],1,n,i);
}
inline void dfs2(int u,int fa){
	if(S[u])dfs2(S[u],u);
	rt[u]=rt[S[u]];ad(u,u,u);
	for(int v:G[u])if(v!=fa&&v!=S[u]){
		dfs2(v,u);
		for(int l=fd3(rt[v],1,n,1),r=fd2(rt[v],1,n,l)-1;l<=n;l=fd3(rt[v],1,n,r+1),r=fd2(rt[v],1,n,l)-1) ad(u,l,r);
		del(rt[v]);
	}
}
struct Q{ int l,r,k,id; }a[N];
namespace ZKW{
	const int M=1<<19;
	int t[M<<1];
	inline void clr(){memset(t,0,sizeof t);}
	inline void ad(int x,int k){
		for(x+=M;x;x>>=1)t[x]=max(t[x],k);
	}
	inline int qr(int l,int r){
		int res=0;
		for(l+=M-1,r+=M+1;l^r^1;l>>=1,r>>=1){
			if(~l&1)res=max(res,t[l^1]);
			if(r&1) res=max(res,t[r^1]);
		}
		return res;
	}
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	rep(i,1,n-1){
		int u=read(),v=read();
		G[u].pb(v),G[v].pb(u);
	}
	dfs1(1,0);
	dfs2(1,0);
	m=read();
	rep(i,1,m)a[i].l=read(),a[i].r=read(),a[i].k=read(),a[i].id=i;
	sort(b+1,b+ct+1,[](Node A,Node B){return A.r-A.l+1>B.r-B.l+1;});
	sort(a+1,a+m+1,[](Q A,Q B){return A.k>B.k;});
	int j=1;
	rep(i,1,m){
		for(;j<=ct&&b[j].r-b[j].l+1>=a[i].k;++j)ZKW::ad(b[j].l,b[j].x);
		int id=a[i].id;
		ans[id]=max(ans[id],ZKW::qr(a[i].l,a[i].r-a[i].k+1));
	}
	ZKW::clr();
	sort(b+1,b+ct+1,[](Node A,Node B){return A.l<B.l;});
	sort(a+1,a+m+1,[](Q A,Q B){return A.l<B.l;});
	j=1;
	rep(i,1,m){
		for(;j<=ct&&b[j].l<=a[i].l;++j)ZKW::ad(b[j].r,b[j].x);
		int id=a[i].id;
		ans[id]=max(ans[id],ZKW::qr(a[i].l+a[i].k-1,n));
	}
	rep(i,1,m)printf("%d\n",ans[i]);
	return 0;
}
