//基本思路：树链剖分 + 线段树（因为忘记倍增怎么写了） 
//O(n^2 + n^q) 或者 O(n log^2 n + q log^2 n) 
//期望得分：32 pts 
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
using namespace std;
const int N = 500050, M = 5050;
vector <int> e[N];
int n, q, siz[N], f[N], top[N], dep[N], dfn[N], son[N], ans, Accepted, l, r, k, ff[M][M], h;
int du[N], maxn, Lca[N << 2];
//重链剖分 
void dfs1(int x, int depth)
{
	dep[x] = depth;
	siz[x] = 1;
	if (x != 1 && e[x].size() == 1)
		return;
	for (auto i : e[x])
	{
		if (i == f[x])
			continue;
		f[i] = x;
		dfs1(i, depth + 1);
		siz[x] += siz[i];
		if (siz[i] > siz[son[x]])
			son[x] = i;
	}
	return; //好习惯 void 要写 return 
}
void dfs2(int x, int fa)
{
	dfn[++Accepted] = x;
	top[x] = fa;
	if (son[x] == 0)
		return;
	dfs2(son[x], fa);
	for (auto i : e[x])
	{
		if (i == f[x] || i == son[x])
			continue;
		dfs2(i, i);
	}
	return; //好习惯 void 要写 return 
}
// O(log n) 的 lca 
int lca(int x, int y)
{
	while(top[x] != top[y])
	{
		if (dep[top[x]] < dep[top[y]])
			swap(x, y);
		x = f[top[x]];
	}
	if (dep[x] > dep[y])
		return y;
	return x;
}
//线段树维护 lca[l, r] 
void build(int root, int l, int r)
{
	if (l == r)
	{
		Lca[root] = l;
		return;
	}
	build(root << 1, l, mid);
	build(root << 1 | 1, mid + 1, r);
	Lca[root] = lca(Lca[root << 1], Lca[root << 1 | 1]);
	return;
}
int query(int root, int l, int r, int x, int y)
{
	if (l >= x && y >= r)
		return Lca[root];
	int u = 0;
	if (mid >= x)
		u = query(root << 1, l, mid, x, y);
	if (mid < y)
		if (u != 0)
			u = lca(query(root << 1 | 1, mid + 1, r, x, y), u);
		else
			u = query(root << 1 | 1, mid + 1, r, x, y);
	return u;
}
int main()
{
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d", &n);
	for (int i = 1, u, v; i < n; i++)
	{
		scanf("%d%d", &u, &v);
		e[u].push_back(v);
		e[v].push_back(u);
		du[u]++;
		du[v]++;
		maxn = max(max(du[u], du[v]), maxn);
	}
	dfs1(1, 1);
	dfs2(1, 1);
	if (n > 5000)
	{
		scanf("%d", &q);
		build(1, 1, n);
		while(q--)
		{
			scanf("%d%d%d", &l, &r, &k);
			ans = 0;
			for (int i = l; i + k - 1 <= r; i++)
				ans = max(ans, dep[query(1, 1, n, i, i + k - 1)]); 
			printf("%d\n", ans);
		}
		return 0;
	}
	for (int i = 1; i <= n; i++)
	{
		h = i;
		ff[i][i] = i;
		for (int j = i + 1; j <= n; j++)
		{
			h = lca(h, j);
			ff[i][j] = h;
		}
	}
	scanf("%d", &q);
	while(q--)
	{
		scanf("%d%d%d", &l, &r, &k);
		ans = 0;
		for (int i = l; i + k - 1 <= r; i++)
			ans = max(ans, dep[ff[i][i + k - 1]]); 
		printf("%d\n", ans);
	}
	return 0; //好习惯 main写 return 0; 
}
