#include<bits/stdc++.h>
using namespace std;
struct node{
	int data,next,pre;
	int du;
}l[1000000];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	int u,v,n,q;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>u>>v;
		l[i].data=u;
		l[i].next=v;
		l[i].du=i;
	}
	cin>>q;
	while(q--)
	{
		int l,r,k;
		cin>>l>>r>>k;
		cout<<l[r].du<<endl;
	}
	return 0;
}
