#include <bits/stdc++.h>
#define int long long
#define F first
#define S second

using namespace std;

int ll[500005];
int n , u , v , q , l , r , k;
vector<int> arr[500005];
int d[500005] , st[500005][15] , sst[500005][15];

 // bro literally forgot how to sprase table
 
void dfs(int at , int pre , int ds)
{
	d[at] = ds;
	for (int i = 0; i < arr[at].size(); i++)
		if (arr[at][i] != pre) dfs(arr[at][i] , at , ds+1);
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
//	freopen("query.in","r",stdin);
//	freopen("query.out","w",stdout);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> u >> v;
		arr[u].push_back(v);
		arr[v].push_back(u);
	}
	dfs(1,-1,1);
	for (int i = 1; i <= n; i++)
		st[i][0] == d[i] , sst[i][0] == d[i];
	for (int j = 1; j <= log(n); j++)
	{
		for (int i = 1; i <= n-(1<<j); i++)
			st[i][j] = max(st[i][j-1],st[i+(1<<(j-1))][j-1]) , sst[i][j] = min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
	}
	cin >> q;
	while (q--)
	{
		cin >> l >> r >> k;
		int ans = -1 , s;
		for (int i = l; i <= r-k; i++) // O(qlogn) solution(for A) just vanished (saying as if such thing ever existed)
			s = log(i+k) , ans = max(ans , min(sst[i][s] , sst[i+k-(1<<s)][s]));
		cout << ans << '\n';
	}
	return 0;
}
