#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=(a);i<=(n);++i)
#define per(i,a,n) for(int i=(n);i>=(a);--i)
#define SZ(x) ((int)(x.size()))

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=5e5+10,lgn=21;

template<typename T>
inline void rd(T &x){
	x=0;
	bool w=false;
	char ch=getchar();
	while(!isdigit(ch)){
		w|=ch=='-';
		ch=getchar();
	} 
	while(isdigit(ch)){
		x=x*10+ch-'0';
		ch=getchar();
	}
	if(w) x=-x;
}

int n,q,idx,dep[maxn],st[lgn][maxn],dfn[maxn];
vector<int> e[maxn];

inline void dfs(int u,int fa){
	st[0][dfn[u]=++idx]=fa;
	dep[u]=dep[fa]+1;
	for(auto v:e[u]){
		if(v==fa) continue;
		dfs(v,u);
	}
}

inline int cmp(int u,int v){
	return dfn[u]<dfn[v]?u:v;
}

inline int lca(int u,int v){
	if(u==v) return u;
	if((u=dfn[u])>=(v=dfn[v])) swap(u,v);
	int i=__lg(v-u++);
	return cmp(st[i][u],st[i][v-(1<<i)+1]);	
} 

namespace sub_1_5{
	#define N 5010
	#define lgN 14
	int pre[N][N],mx[lgN][N],res[N];
	vector<array<int,3>> event[N];
	
	inline int query(int l,int r){
		int i=__lg(r-l+1);
		return max(mx[i][l],mx[i][r-(1<<i)+1]);
	}
	
	inline void sol(){
		rd(q);		
		rep(l,1,n){
			int d=l;
			pre[l][1]=dep[d];
			rep(r,l+1,n){
				d=lca(d,r);
				pre[l][r-l+1]=dep[d];
			}
		}
		rep(i,1,q){
			int l,r,k;
			rd(l); rd(r); rd(k);
			event[r].push_back({l,k,i});
		}
		per(k,1,n){
			rep(i,1,n) mx[0][i]=pre[i][k];
			rep(i,1,13)
				for(int j=1;j+(1<<i)-1<=n;++j)
					mx[i][j]=max(mx[i-1][j],mx[i-1][j+(1<<(i-1))]);
			rep(i,1,n){
				for(auto x:event[i]){
					int j=x[0],dis=x[1],id=x[2];
					if(dis==k) res[id]=query(j,max(j,i-dis+1));
				}
			}
		}
		rep(i,1,q) printf("%d\n",res[i]);
	}
}

inline void solve(){
	rd(n);
	rep(i,1,n-1){
		int u,v;
		rd(u); rd(v);
		e[u].emplace_back(v);
		e[v].emplace_back(u); 
	}	
	dfs(1,0);
	rep(i,1,20) 
		for(int j=1;j+(1<<i)-1<=n;++j)
			st[i][j]=cmp(st[i-1][j],st[i-1][j+(1<<(i-1))]);	
	if(n<=5000){
		sub_1_5::sol();
		return;
	}

}

signed main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	int _=1;
//	scanf("%d",&_);
	while(_--) solve();
}

