#include<bits/stdc++.h>
#define ci const int
#define ll long long
using namespace std;
int read(){
	char ch=getchar();
	int res=0;
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9')res=(res<<1)+(res<<3)+(ch^48),ch=getchar();
	return res;
}
void out(ci x){
	if(x>9)out(x/10);
	putchar(x%10+48);
}
ci N=5e5+5;
int n,q,lca[5005][5005],id[N],f[N],dep[N],gt[5005][5005],fa[N][19];
vector<int>g[N];
void dfs(ci x){
	dep[x]=dep[f[x]]+1;
	fa[x][0]=f[x];
	for(int i=1;i<19;++i)fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int y:g[x])
		if(y!=f[x])
			f[y]=x,dfs(y);
}
inline bool cmp(ci x,ci y){
	return dep[x]<dep[y];
}
int L(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	for(int i=18;~i;--i)if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
	if(x==y)return x;
	for(int i=18;~i;--i)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
int st[N][19],lg[N];
int qry(ci l,ci r){
	ci k=lg[r-l+1];
	return L(st[l][k],st[r-(1<<k)+1][k]);
}
void Solve(){
	lg[0]=-1;
	for(int i=1;i<=n;++i)st[i][0]=i,lg[i]=lg[i>>1]+1;
	for(int j=1;j<19;++j)
		for(int i=1;i+(1<<j)-1<=n;++i)
			st[i][j]=L(st[i][j-1],st[i+(1<<j-1)][j-1]);
	for(int q=read();q;--q){
		ci l=read(),r=read(),k=read();
		out(dep[qry(l,r)]),putchar(10);
	}
}
int deg[N],ans[N];
struct node{
	int id,l,r,k;
	node(int l=0,int r=0,int k=0):l(l),r(r),k(k){}
}nd[N],ndl[N],ndr[N];
ci M=N<<2;
#define ls k<<1
#define rs k<<1|1
struct sec{
	int ans,l,r;bool ok;
	sec(int ans=0,int l=0,int r=0,bool ok=0):ans(ans),l(l),r(r),ok(ok){}
	inline sec operator + (const sec &t)const{
		return sec(max(max(ans,t.ans),r+t.l),ok?(l+t.l):l,t.ok?(t.r+r):t.r,ok&&t.ok);
	}
};
struct Tree{
	int l[M],r[M];
	sec s[M];
	void upd(ci k){
		s[k]=s[ls]+s[rs];
	}
	void build(ci k,ci L,ci R){
		l[k]=L,r[k]=R;
		if(L==R){
			s[k]=sec(0,0,0,0);
			return;
		}
		ci mid=L+R>>1;
		build(ls,L,mid),build(rs,mid+1,R);
		upd(k);
	}
	void modify(ci k,ci p,ci v){
		if(l[k]==r[k]){
			if(v==1)s[k]=sec(1,1,1,1);
			else s[k]=sec(0,0,0,0);
			return;
		}
		modify(r[ls]>=p?ls:rs,p,v),upd(k);
	}
	sec qry(ci k,ci L,ci R){
		if(L<=l[k]&&r[k]<=R)return s[k];
		if(R<=r[ls])return qry(ls,L,R);
		if(L>=l[rs])return qry(rs,L,R);
		return qry(ls,L,R)+qry(rs,L,R);
	}
}T;
int seg[N];
void Calc(ci l,ci r,ci ql,ci qr){//>r -> in
	if(ql>qr)return;
	if(l==r){
		for(int i=ql;i<=qr;++i)ans[nd[i].id]=l;
		return;
	}
	ci mid=l+r>>1;
	for(int i=mid+1;i<=r;++i)T.modify(1,seg[i],1);
	int l1=0,l2=0;
	for(int i=ql;i<=qr;++i)
		if(T.qry(1,nd[i].l,nd[i].r).ans>=nd[i].k)ndr[++l2]=nd[i];
		else ndl[++l1]=nd[i];
	int cur=ql-1;
	for(int i=1;i<=l1;++i)nd[++cur]=ndl[i];
	for(int i=1;i<=l2;++i)nd[++cur]=ndr[i];
	Calc(l,mid,ql,ql+l1-1);
	for(int i=mid+1;i<=r;++i)T.modify(1,seg[i],0);
	Calc(mid+1,r,ql+l1,qr);
}
void Solve2(){
	// int S=clock();
	q=read();
	for(int i=1;i<=q;++i)nd[i].l=read(),nd[i].r=read(),nd[i].k=read(),nd[i].id=i;
	for(int i=1;i<=n;++i)seg[dep[i]]=i;
	T.build(1,1,n);
	Calc(1,n,1,q);
	for(int i=1;i<=q;++i)out(ans[i]),putchar(10);
	// printf("%d ms.\n",clock()-S);
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	for(int i=1,x,y;i<n;++i)
		x=read(),y=read(),
		++deg[x],++deg[y],
		g[x].push_back(y),
		g[y].push_back(x);
	dfs(1);
	bool sp=deg[1]==1;
	for(int i=2;i<=n;++i)sp&=deg[i]<=2;
	if(n>5000&&sp){
		Solve2();
		return 0;
	}
	if(n>5000){
		Solve();
		return 0;
	}
	for(int i=1;i<=n;++i)id[i]=i;
	sort(id+1,id+n+1,cmp);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j){
			if(i==j)lca[id[i]][id[j]]=id[i];
			else if(dep[id[i]]>=dep[id[j]])lca[id[i]][id[j]]=lca[f[id[i]]][id[j]];
			else lca[id[i]][id[j]]=lca[id[i]][f[id[j]]];
		}
	for(int l=1;l<=n;++l){
		gt[l][l]=l;
		for(int r=l+1;r<=n;++r)gt[l][r]=lca[gt[l][r-1]][r];
	}
	for(int q=read();q;--q){
		ci l=read(),r=read(),k=read();
		int ans=0;
		for(int i=l;i+k-1<=r;++i)ans=max(ans,dep[gt[i][i+k-1]]);
		out(ans),putchar(10);
	}
	return 0;
}