#include<bits/stdc++.h>
using namespace std;
int n,cnt,m,k[10000001],head[10000001],dep[10000001];
void dfs(int id,int fa){
	dep[id] = dep[fa+1];
//	cout << " eqfeqf" <<head[id] <<" ";
	dfs(head[id],id);
	return ;
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin >> n;
	for(int i = 1; i < n; i++){
		int a,b;
		cin >> a >> b;
		head[a] = b;
	}
	dep[0] = 0;
	dfs(1,0);
	cin >> m;
	for(int i = 1; i <= m; i++){
		int l,r,k,ans = 0;
		cin >> l >> r >> k;
		for(int o = l; o <= r; o++){
			for(int p = l; p <= r; p++){
				if(abs(dep[o]-dep[p]) >= k){
					ans = max(ans,max(dep[o],dep[p]));
				}
			}
		}
		cout << ans <<endl;
	}
	return 0;
}

