#include<bits/stdc++.h>
using namespace std;
int g,n,l,r,Q,K;
struct dian
{
	int f;
	int s;
}nn[500005];
int cs[500005];
int s1,s2,s3;
void dfs1(int j,int i)
{
	cs[i]=j;
	if(nn[i].s)
	{
		dfs1(j+1,nn[i].s);
	}
}
int main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin>>n;
	for(int i=1,u,v;i<=n;i++)
	{
		cin>>u>>v;
		nn[v].s=u,nn[u].f=v;
	}
	for(int i=1;i<=n;i++)
	{
		if(!nn[i].f)
		{
			g=i;
		}
	}
	dfs1(1,g);
	for(cin>>Q;Q;Q--)
	{
		cin>>l>>r>>K;
		s1=s2=s3=-1;
		for(int k=K;k<=r-l+1;k++)
		{
			s1=max(s1,s2);
			for(int i=l;i<=r-k+1;i++)
			{
				s2=max(s3,s2);
				for(int j=i+1;j<=i+k-1;j++)
				{
					s3=max(s3,min(cs[i],cs[j]));
				}
			}
		}
		cout<<s1<<"\n";
	}
	return 0;
}
