#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int n,q;
int a[maxn];
int b[maxn];
inline int rd(){
	int x=0,f=1;char s=getchar();
	while(s<'0'||s>'9'){
		if(s=='-')f=-1;
		s=getchar();
	}
	while(s>='0'&&s<='9'){
		x=x*10+s-'0';
		s=getchar();
	}
	return x*f;
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=rd();
	for(int i=1;i<n;i++){
		int u=rd(),v=rd();
		b[u]=v;
	}
	a[1]=1;int now=1;
	while(b[now]!=0){
		a[b[now]]=a[now]+1;
		now=b[now];
	}
//	for(int i=1;i<=n;i++){
//		cout<<a[i]<<endl;
//	}
	q=rd();
	while(q--){
		int l=rd(),r=rd(),k=rd(),ans=-1e9;
		for(int i=l;i<=(r-k+1);i++){
			int minn=1e9;
			for(int j=i;j<=i+k-1;j++){
				minn=min(minn,a[j]);	
			}
			ans=max(minn,ans);
		}
		cout<<ans<<endl;
	}
	//system("pause");
	return 0;
}
