#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
inline ll read(){
	ll x=0;
	int c=getchar();
	bool f=0;
	while(c<48||c>57) f=(c=='-'),c=getchar();
	while(c>47&&c<58) x=(x<<3)+(x<<1)+(c&15),c=getchar();
	return f?-x:x;
}
inline void write(ll x,char end='\n'){
	if(x==0){
		putchar('0');
		putchar(end);
		return;
	}
	if(x<0) putchar('-'),x=-x;
	short ch[30]={0},cnt=0;
	while(x) ch[cnt++]=x%10,x/=10;
	while(cnt--) putchar(ch[cnt]^48);
	putchar(end);
}
const int N=5e5+5;
int n,q;
struct node{int to,nxt;}e[N<<1];
int head[N],cnt;
inline void add(int u,int v){
	e[++cnt]={v,head[u]};
	head[u]=cnt;
}
int dep[N],dfn[N],ed[N],todfn[N<<1],tot;
int lg[N<<1],mi[22][N<<1];
inline void dfs(int u,int fa){
	dep[u]=dep[fa]+1;
	dfn[u]=ed[u]=++tot;
	todfn[tot]=u;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==fa) continue;
		dfs(v,u);
		ed[u]=++tot;
		todfn[tot]=u;
	}
}
inline int get(int x,int y){return dep[x]<dep[y]?x:y;}
inline void initlca(){
	lg[0]=-1;
	for(int i=1;i<=n<<1;++i) lg[i]=lg[i>>1]+1;
	for(int i=1;i<=tot;++i) mi[0][i]=todfn[i];
	for(int i=1;i<=lg[tot];++i){
		for(int j=1;j+(1<<i)-1<=tot;++j)
			mi[i][j]=get(mi[i-1][j],mi[i-1][j+(1<<(i-1))]);
	}
}
inline int getlca(int u,int v){
	if(u==v) return u;
	if(dfn[u]>dfn[v]) swap(u,v);
	u=dfn[u],v=ed[v];
	int d=lg[v-u+1];
	return get(mi[d][u],mi[d][v-(1<<d)+1]);
}
int treew[N<<2],treew2[N<<2];
int tl[N],tr[N];
inline void pushup(int u){treew[u]=getlca(treew[u<<1],treew[u<<1|1]);}
inline void build(int u,int l,int r){
	if(l==r){
		treew[u]=l;
		return;
	}
	int mid=(l+r)>>1;
	build(u<<1,l,mid);
	build(u<<1|1,mid+1,r);
	pushup(u);
}
inline int query(int u,int l,int r,int ql,int qr){
	if(ql<=l&&r<=qr) return treew[u];
	int mid=(l+r)>>1;
	if(qr<=mid) return query(u<<1,l,mid,ql,qr);
	if(mid<ql) return query(u<<1|1,mid+1,r,ql,qr);
	return getlca(query(u<<1,l,mid,ql,qr),query(u<<1|1,mid+1,r,ql,qr));
}
inline void solve1(){
	build(1,1,n);
	int l,r,k;
	while(q--){
		l=read(),r=read(),k=read();
		int ans=0;
		for(int i=l;i+k-1<=r;++i)
			ans=max(ans,dep[query(1,1,n,i,i+k-1)]);
		write(ans);
	}
}
int ans[N];
struct qrys{
	int l,r,k,id;
	friend bool operator <(qrys x,qrys y){
		return x.k>y.k;
	}
}qry[N];
struct pt{int len,x;}nw[N];
inline bool cmp2(pt x,pt y){return x.len>y.len;}
inline void pushup2(int u){treew2[u]=max(treew2[u<<1],treew2[u<<1|1]);}
inline void modify(int u,int l,int r,int pos,int x){
	if(l==r){
		treew2[u]=max(treew2[u],x);
		return;
	}
	int mid=(l+r)>>1;
	if(pos<=mid) modify(u<<1,l,mid,pos,x);
	else modify(u<<1|1,mid+1,r,pos,x);
	pushup2(u);
}
inline int query2(int u,int l,int r,int ql,int qr){
	if(ql<=l&&r<=qr) return treew2[u];
	int mid=(l+r)>>1;
	if(qr<=mid) return query2(u<<1,l,mid,ql,qr);
	if(mid<ql) return query2(u<<1|1,mid+1,r,ql,qr);
	return max(query2(u<<1,l,mid,ql,qr),query2(u<<1|1,mid+1,r,ql,qr));
}
inline void solve2(){
	build(1,1,n);
	for(int i=1;i<=n;++i){
		tl[i]=tr[i]=i;
		while(tl[i]>1&&getlca(tl[i]-1,i)==i) tl[i]--;
		while(tr[i]<n&&getlca(tr[i]+1,i)==i) tr[i]++;
//		printf("tl[%d]=%d, tr[%d]=%d, dep=%d\n",i,tl[i],i,tr[i],dep[i]);
	}
	int qtot=0;
	int l,r,k;
	for(int t=1;t<=q;++t){
		l=read(),r=read(),k=read();
		if(k+10>=r-l+1){
			for(int i=l;i+k-1<=r;++i)
				ans[t]=max(ans[t],dep[query(1,1,n,i,i+k-1)]);
			continue;
		}
		qry[++qtot]={l,r,k,t};
	}
	sort(qry+1,qry+qtot+1);
	for(int i=1;i<=n;++i) nw[i]={tr[i]-tl[i]+1,dep[i]};
	sort(nw+1,nw+n+1,cmp2);
	for(int i=1,now=1;i<=qtot;++i){
		while(now<=n&&nw[now].len>=qry[i].k){
			modify(1,1,n,i,nw[now].x);
			++now;
		}
		int gx=query2(1,1,n,qry[i].l,qry[i].r);
		if(!gx) ans[qry[i].id]=query2(1,1,n,1,n);
		else ans[qry[i].id]=gx;
	}
	for(int i=1;i<=q;++i) write(ans[i]);
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	for(int i=1,u,v;i<n;++i){
		u=read(),v=read();
		add(u,v);add(v,u);
	}
	dfs(1,0);
	initlca();
	q=read();
	if(n<=5000&&q<=5000){
		solve1();
		return 0;
	}
	solve2();
	return 0;
}
