#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+10;
const int INF = 1e9;
int n,q;
vector<int>vec[maxn];
int dep[maxn];
struct tree{
	int mi;
};
struct Tree{
	tree t[maxn];
	void pushup(int id){
		t[id].mi = min(t[id<<1].mi,t[id<<1|1].mi);
	}
	void build(int id,int l,int r){
		if(l==r){
			t[id].mi = dep[l];
			return ;
		}
		int mid = l+r>>1;
		build(id<<1,l,mid);
		build(id<<1|1,mid+1,r);
		pushup(id);
	}
	int Min(int id,int l,int r,int L,int R){
		if(l>R||r<L)return INF;
		if(L<=l&&r<=R){
			return t[id].mi;
			
		}
		int mid = l+r>>1;
		return min(Min(id<<1,l,mid,L,R),Min(id<<1|1,mid+1,r,L,R));
	}
}T;
int f[maxn][22];
void dfs(int x,int fa){
	dep[x]=dep[fa]+1;
	f[x][0]=fa;
	for(auto to:vec[x]){
		if(to==fa)continue;
		dfs(to,x);
	}
}
void init(){
	for(int j = 1;j<=20;j++){
		for(int i = 1;i<=n;i++){
			f[i][j] = f[f[i][j-1]][j-1];
		}
	}
}
int LCA(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	for(int i = 20;~i;i--){
		if(dep[f[u][i]]>=dep[v])u = f[u][i];
	}
	if(u==v)return u;
	for(int i = 20;~i;i--){
		if(f[u][i]!=f[v][i])u = f[u][i],v = f[v][i];
	}
	return f[u][0];
}
int ycl[5010][5010];
void solve(){
	init();
	for(int i = 1;i<=n;i++){
		int lca = i;
		ycl[i][1]=dep[lca];
		for(int j = i+1;j<=n;j++){
		//	cout<<lca<<endl;
			lca = LCA(lca,j);
			ycl[i][j-i+1]=dep[lca];
		}
	}
	cin>>q;
	while(q--){
		int l,r,k;cin>>l>>r>>k;
		int ans = 0;
		for(int i = l;i<=r-k+1;i++){
			ans= max(ans,ycl[i][k]);
		}
		cout<<ans<<'\n';
	}
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i = 1,u,v;i<n;i++){
		cin>>u>>v;
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	dfs(1,0);

	if(n<=5000){
		solve();
		return 0;
	}
	T.build(1,1,n);
//	cout<<233<<endl;
	cin>>q;
	while(q--){
		int l,r,k;cin>>l>>r>>k;
		cout<<T.Min(1,1,n,l,r)<<'\n';
	}	
	 	
	
}
