#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((l + r) >> 1)
using namespace std;
const int maxn = 1e5+5;
int gt[maxn] = {0}, dep[maxn];

struct seg
{
	int t[maxn << 2];
	void up(int p)
	{
		t[p] = min(t[p << 1], t[p << 1 | 1]);
	}
	void build(int l,int r,int p)
	{
		if(l == r)
			t[p] = dep[l];
		else
		{
			build(l, mid, p << 1);
			build(mid + 1, r, p << 1 | 1);
			up(p);
		}
	}
	int query(int l,int r,int p,int L,int R)
	{
		if(L <= l && r <= R)
			return t[p];
		else
		{
			int ans = 0x3f3f3f3f;
			if(L<=mid)
				ans = min(ans, query(l, mid, p << 1, L, R));
			if(R>mid)
				ans = min(ans, query(mid + 1, r, p << 1 | 1, L, R));
			return ans;
		}
	}
}tr;

int main()
{
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	memset(tr.t, 0x3f3f3f3f, sizeof(tr.t));
	int n, q;
	scanf("%d", &n);
	for(int i = 2; i <= n; i++)
	{
		int x,y;
		scanf("%d%d", &x, &y);
		gt[x] = y;
	}
	dep[1] = 1;
	int x = 1;
	while(x != 0)
	{
		dep[gt[x]] = dep[x] + 1;
		x = gt[x];
	}
	tr.build(1,n,1);
	scanf("%d", &q);
	while(q--)
	{
		int x,y,k;
		scanf("%d%d%d",&x,&y,&k);
//		int ans = 0x3f3f3f3f;
//		for(int i = x; i <= y; i ++)
//			ans = min(dep[i], ans);
		printf("%d\n", tr.query(1,n,1,x,y));
	}
	return 0;
}
