#include <bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define _rep(i,a,b) for(int i=(a);i>=(b);--i)
#define eb emplace_back
#define pii pair<int,int>
#define fi first
#define se second
#define MISAKA main
#define FIO(FILE) freopen(FILE".in","r",stdin),freopen(FILE".out","w",stdout)
using namespace std;
bool __st;
inline int read(){
	char c=getchar();int x=0,f=1;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while('0'<=c&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}
const int N=5e5+10,mod=998244353;
int n,q,dfn[N],tim,id[N],siz[N],dep[N],son[N],fa[N],f[5010][5010],ans[N];
int st1[N][20],st2[N][20],lg[N];
int mx(int l,int r){int k=lg[r-l+1];return max(st1[l][k],st1[r-(1<<k)+1][k]);}
int mn(int l,int r){int k=lg[r-l+1];return min(st2[l][k],st2[r-(1<<k)+1][k]);}
vector<int> g[N];set<int> s;
struct node{
	int x,y,k,id;
	node(int a=0,int b=0,int c=0,int d=0){
		x=a,y=b,k=c,id=d;
	}
};vector<node> v1,v2,v3,v4;
vector<pii> Q[N],vt[N];
bool cmp(node a,node b){return a.k>b.k;}
void dfs(int u,int f){
	fa[u]=f,dep[u]=dep[f]+1,siz[u]=1;
	id[dfn[u]=++tim]=u;
	for(int v:g[u])if(v!=f){
		dfs(v,u),siz[u]+=siz[v];
		if(siz[v]>siz[son[u]]) son[u]=v;
	}
}
void solve(int x,int L,int R,int d){
	int l=x,r=n,p=-1,q=-1;
	while(r>=l){
		int mid=l+r>>1;
		if(mn(x,mid)<L||mx(x,mid)>R) r=mid-1;
		else l=mid+1;
	}
	auto it=s.upper_bound(r);
	if(it!=s.begin()) v1.eb(x,d,*(--it)-x+1,0),q=*it;
	l=1,r=x;
	while(r>=l){
		int mid=l+r>>1;
		if(mn(mid,x)<L||mx(mid,x)>R) l=mid+1;
		else r=mid-1;
	}
	it=s.lower_bound(l);
	if(it!=s.end()) v2.eb(x,d,x-*it+1,0),p=*it;
	if(p!=-1&&q!=-1){
		vt[q].eb(p,d);
		v1.eb(p,d,q-p+1,0);
		v2.eb(q,d,q-p+1,0);
	}
}
void dfs(int u,int f,int tp){
	for(int v:g[u])if(v!=f&&v!=son[u]) dfs(v,u,0);
	if(son[u]) dfs(son[u],u,1);
	for(int v:g[u])if(v!=f&&v!=son[u])
		rep(i,dfn[v],dfn[v]+siz[v]-1) s.insert(id[i]);
	for(int v:g[u])if(v!=f&&v!=son[u]){
		rep(i,dfn[v],dfn[v]+siz[v]-1) s.erase(id[i]);
		rep(i,dfn[v],dfn[v]+siz[v]-1) 
			solve(id[i],dfn[u],dfn[u]+siz[u]-1,dep[u]);
		rep(i,dfn[v],dfn[v]+siz[v]-1) s.insert(id[i]);
	}
	solve(u,dfn[u],dfn[u]+siz[u]-1,dep[u]);s.insert(u);
	v1.eb(u,dep[u],1,0),v2.eb(u,dep[u],1,0);
	if(!tp) s.clear();
}
struct segtree{int l,r,mx;}t[N<<2];
void bd(int x,int l,int r){
	t[x].l=l,t[x].r=r,t[x].mx=0;
	if(l==r) return;int mid=l+r>>1;
	bd(2*x,l,mid);bd(2*x+1,mid+1,r);
}
void upd(int x,int p,int k){
	t[x].mx=max(t[x].mx,k);
	if(t[x].l==t[x].r) return;
	upd(2*x+(p>t[2*x].r),p,k);
}
int qry(int x,int l,int r){
	if(t[x].l>r||t[x].r<l) return 0;
	if(l<=t[x].l&&t[x].r<=r) return t[x].mx;
	return max(qry(2*x,l,r),qry(2*x+1,l,r));
}
void misaka(){
	n=read();
	rep(i,2,n){
		int u=read(),v=read();
		g[u].eb(v);g[v].eb(u);
		lg[i]=lg[i>>1]+1;
	}
	q=read();
	dfs(1,0);
	if(n<=5000&&q<=5000){
		rep(i,1,n){
			f[i][i]=dep[i];
			int rt=i;
			rep(j,i+1,n){
				int x=rt,y=j;
				for(;x^y;x=fa[x])if(dep[x]<dep[y]) swap(x,y);
				rt=dep[x]<dep[y]?x:y,f[i][j]=dep[rt];
			}
		}
		while(q--){
			int l=read(),r=read(),k=read(),mx=0;
			rep(i,l,r-k+1) mx=max(mx,f[i][i+k-1]);
			printf("%d\n",mx);
		}
		return;
	}
	rep(i,1,n) st1[i][0]=st2[i][0]=dfn[i];
	rep(j,1,19)rep(i,1,n-(1<<j)+1){
		st1[i][j]=max(st1[i][j-1],st1[i+(1<<j-1)][j-1]);
		st2[i][j]=min(st2[i][j-1],st2[i+(1<<j-1)][j-1]);
	}
	dfs(1,0,1);
	rep(i,1,q){
		int l=read(),r=read(),k=read();
		Q[r].eb(l,i);
		v3.eb(l,r-k+1,k,i),v4.eb(l+k-1,r,k,i);
	}
	sort(v1.begin(),v1.end(),cmp);
	sort(v2.begin(),v2.end(),cmp);
	sort(v3.begin(),v3.end(),cmp);
	sort(v4.begin(),v4.end(),cmp);
	int j=0,sz=v3.size();
	bd(1,1,n);
	rep(i,0,sz-1){
		while(j<v1.size()&&v1[j].k>=v3[i].k) 
			upd(1,v1[j].x,v1[j].y),j++;
		ans[v3[i].id]=max(ans[v3[i].id],qry(1,v3[i].x,v3[i].y));
	}
	bd(1,1,n);
	j=0,sz=v4.size();
	rep(i,0,sz-1){
		while(j<v2.size()&&v2[j].k>=v4[i].k) 
			upd(1,v2[j].x,v2[j].y),j++;
		ans[v4[i].id]=max(ans[v4[i].id],qry(1,v4[i].x,v4[i].y));
	}
	bd(1,1,n);
	_rep(i,n,1){
		for(pii x:vt[i]) upd(1,x.fi,x.se);
		for(pii x:Q[i]) ans[x.se]=max(ans[x.se],qry(1,1,x.fi));
	}
	rep(i,1,q) printf("%d\n",max(1,ans[i]));
}
bool __ed;
signed MISAKA(){
	FIO("query");
//	system("fc query4.out query4.ans");
	int T=1;
	while(T--) misaka();
	return 0;
}

