#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e5+5,M=1e9+7;
int n,q,k;
int dfn[N],dep[N],id;
int st[30][N],st2[30][N];
vector<int>vec[N];
int merge(int u,int v){
	return dep[u]<dep[v]?u:v;
}
void dfs(int u,int fa){
	dfn[u]=++id,dep[u]=dep[fa]+1;
	st[0][id]=fa;
	for(int v:vec[u]){
		if(v==fa)continue;
		dfs(v,u);
	}
}
inline int get_lca(int u,int v){
	if(u==v)return u;
	if(dfn[u]>dfn[v])swap(u,v);
	u=dfn[u]+1,v=dfn[v];
	int m=log2(v-u+1);
	return merge(st[m][u],st[m][v-(1<<m)+1]);
}
inline int get_range(int l,int r){
	int m=log2(r-l+1);
	return get_lca(st2[m][l],st2[m][r-(1<<m)+1]);
}
void solve0(){
	scanf("%d",&q);
	int l,r,k,ans;
	while(q--){
		ans=0;
		scanf("%d%d%d",&l,&r,&k);
		for(int i=l;i+k-1<=r;i++){
			ans=max(ans,dep[get_range(i,i+k-1)]);
		}
		printf("%d\n",ans);
	}
}
struct A{
	int l,r,k,i;
}b[N];
bool cmp(A x,A y){
	return x.k<y.k;
}
int mx[N<<2],cnt[N],ccf[N];
inline int ls(int p){
	return p<<1;
}
inline int rs(int p){
	return p<<1|1;
}
inline void pushup(int p){
	mx[p]=max(mx[ls(p)],mx[rs(p)]);
}
inline void build(int l,int r,int p){
	mx[p]=0;
	if(l==r){
		mx[p]=dep[get_range(l,l+k-1)];return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,ls(p));
	build(mid+1,r,rs(p));
	pushup(p);
}
inline int query(int l,int r,int p,int ql,int qr){
	if(ql<=l&&r<=qr)return mx[p];
	int mid=(l+r)>>1,s=0;
	if(ql<=mid)s=max(s,query(l,mid,ls(p),ql,qr));
	if(mid<qr)s=max(s,query(mid+1,r,rs(p),ql,qr));
	return s;
}
void solve1(){
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		scanf("%d%d%d",&b[i].l,&b[i].r,&b[i].k);
		cnt[b[i].k]++;
		b[i].i=i;
	}
	sort(b+1,b+1+q,cmp);
	int wtf=10000;
	for(int i=1;i<=q;i++){
		k=b[i].k;
//		if(0){
		if(cnt[k]>100&&k<=n-wtf){
			if(b[i-1].k!=b[i].k)build(1,n-k+1,1);
			ccf[b[i].i]=query(1,n-k+1,1,b[i].l,b[i].r-k+1);
		}else{
			int ans=0;
			for(int j=b[i].l;j+k-1<=b[i].r;j++){
				ans=max(ans,dep[get_range(j,j+k-1)]);
			}
			ccf[b[i].i]=ans;
		}
	}
	for(int i=1;i<=q;i++){
		printf("%d\n",ccf[i]);
	}
}
int main(){//20pts
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	for(int i=1,u,v;i<n;i++){
		scanf("%d%d",&u,&v);
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	dfs(1,0);
	for(int i=1;(1<<i)<=n;i++){
		for(int j=1;j+(1<<i)-1<=n;j++){
			st[i][j]=merge(st[i-1][j],st[i-1][j+(1<<i-1)]);
		}
	}
	for(int i=1;i<=n;i++)st2[0][i]=i;
	for(int i=1;(1<<i)<=n;i++){
		for(int j=1;j+(1<<i)-1<=n;j++){
			st2[i][j]=get_lca(st2[i-1][j],st2[i-1][j+(1<<i-1)]);
		}
	}
	if(n<=1e5)solve0();
	else solve1();
	return 0;
}/*
6
5 6
6 1
6 2
2 3
2 4
1
1 6 3
*/ 
