#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int N = 5e5+5;

int n,m,u,v,l,r,k,ans;
vector<int> e[N];

int dfs(int u,int fa,int dep){
	int re=0;
	if(u>=l&&u<=r)re=1;
	for(int v:e[u]){
		if(v==fa)continue;
		re+=dfs(v,u,dep+1);
	}
	if(re>=k)ans=max(ans,dep);
	return re;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	cin>>m;
	if(m<=5000&&n<=5000){
		for(int i=1;i<=m;i++){
			cin>>l>>r>>k;
			ans=0;
			dfs(1,0,1);
			cout<<ans<<'\n';
		}
	}else{
		
	}
	return 0;
}
