#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;

vector <int> g[N];
int n, q, f[N][25], dep[N];
int A[N][N], B[N][N];
int lca(int x, int y)
{
	if(dep[x] < dep[y]) swap(x, y);
	for(int i = 20;i >= 0;i--)
	{
//		cout << x << ' ' << y << ' ' << i << endl;
		if(dep[f[x][i]] >= dep[y]) x = f[x][i];
		if(x == y) return x;
	}
	for(int i = 20;i >= 0;i--)
	{
		if(f[x][i] != f[y][i])
		{
			x = f[x][i];
			y = f[y][i];
		}
	}
	return f[x][0];
}

void dp(int u, int fa)
{
	dep[u] = dep[fa] + 1;
	f[u][0] = fa;
	for(int v : g[u])
	{
		if(v == fa) continue;
		dp(v, u);
	}
}

int main()
{
	#ifndef SCZ
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	#endif
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1;i < n;i++)
	{
		int _u, _v;
		cin >> _u >> _v;
		g[_u].push_back(_v);
		g[_v].push_back(_u); 
	}
	dp(1, 0);
	for(int j = 1;j <= 20;j++)
	{
		for(int i = 1;i <= n;i++)
		{
			f[i][j] = f[f[i][j - 1]][j - 1];
		}
	}
	for(int l = 1;l <= n;l++)
	{
		int p = 0;
		for(int r = l;r <= n;r++)
		{
			if(!p)
			{
				p = r;
				A[l][r] = dep[p];
			}
			else
			{
				p = lca(p, r);
				A[l][r] = dep[p];
			}
//			cout << l << ' ' << r << ' ' << p << ' ' << dep[p] << ' ' << A[l][r] << endl;
		}
	}
	cin >> q;
	while(q--)
	{
		int l, r, k;
		cin >> l >> r >> k;
		int ans = 0;
		for(int i = l;i <= r;i++)
		{
			for(int j = i;j <= r;j++)
			{
				if(j - i + 1 < k) continue;
//				cout << i << ' ' << j << ' ' << A[i][j] << endl;
				ans = max(ans, A[i][j]);
			}	
		}
		cout << ans << endl;
	} 
	return 0;
}
