#include <bits/stdc++.h>
using namespace std;

const int N = 5e3 + 10;

int n;
basic_string<int> edge[N];
int f[N][20];
int dep[N];
int lca[N][N];

void dfs(int u, int fat)
{
    dep[u] = dep[fat] + 1;
    f[u][0] = fat;
    for (int i = 15; i; i--)
    {
        f[u][i] = f[f[fat][i - 1]][i - 1];
    }
    for (auto v : edge[u])
    {
        if (v != fat)
        {
            dfs(v, u);
        }
    }
}

int LCA(int u, int v)
{
    if (dep[u] < dep[v])
    {
        swap(u, v);
    }
    while (dep[u] > dep[v])
    {
        u = f[u][0];
    }
    if (u == v)
    {
        return u;
    }
    while (f[u][0] != f[v][0])
    {
        u = f[u][0];
        v = f[v][0];
    }
    return f[u][0];
}

int main()
{
    freopen("query.in", "r", stdin);
    freopen("query.out", "w", stdout);
    cin.tie(0)->sync_with_stdio(0);

    cin >> n;
    for (int i = 1, u, v; i < n; i++)
    {
        cin >> u >> v;
        edge[u] += v, edge[v] += u;
    }
    dfs(1, 0);
    LCA(4, 6);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            lca[i][j] = LCA(i, j);
        }
    }
    int q;
    cin >> q;
    while (q--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        int ans = 0;
        for (int i = l, tmp; i + k - 1 <= r; i++)
        {
            tmp = i;
            for (int j = i + 1; j < i + k; j++)
            {
                tmp = lca[tmp][j];
            }
            ans = max(dep[tmp], ans);
        }
        cout << ans << '\n';
    }
}