#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int N=5e5+7;
int n;
int deep[N];
vector<int> g[N];
void dfs(int x,int fa){
	deep[x]=deep[fa]+1;
	for(int i=0;i<g[x].size();i++){
		if(g[x][i]==fa){
			continue;
		}
		dfs(g[x][i],x);
	}
}
int qq[N],h,t;
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin >> n;
	for(int i=1;i<n;i++){
		int u,v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1,0);
	int q;
	cin >> q;
	for(int i=0;i<q;i++){
		int l,r,k;
		cin >> l >> r >> k;
		memset(qq,0,sizeof(qq));
		t=0,h=1;
		int ans=0;
		for(int j=l;j<=r;j++){
			
			while(h<=t && j-qq[h]+1<=k){
				h++;
			}
			int dep=deep[j];
			while(h<=t && deep[qq[t]]>=dep){
				t--;
			}
			qq[++t]=j;
			if(j>=l+k-1){
				ans=max(ans,deep[qq[h]]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
/*
3
1 2
2 3
1
2 3
1
*/
