#include <iostream>
#include <cmath>
#define ll long long
using namespace std;
ll dep[500005],n,q,u,v,l,r,k,st[500005][30];
int main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin>>n;
	dep[1]=1;
	for (int i=1;i<=n-1;i++)
	{
		cin>>u>>v;
		dep[v]=dep[u]+1;
	}
	ll m=log2(n);
	for (ll i=1;i<=n;i++)
		st[i][0]=dep[i];
	for (ll j=1;j<=m;j++)
		for (ll i=1;i<=n-(1<<j)-1;i++)
			st[i][j]=min(st[i][j-1],st[i+1<<(j-1)][j-1]);
	cin>>q;
	while (q--)
	{
		ll ans=n;
		cin>>l>>r>>k;
		ll temp=log2(k);
		for (ll i=l;i<=r-k;i++)
		{
			ans=max(ans,min(st[i][temp],st[i+k-(1<<temp)+1][temp]));
		}
		cout<<ans<<endl;
	}
	return 0;
}
