#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 5e3 + 10, oo = 1e18;
int n, q, f[N][20], dep[N], qq[N][N];
vector<int> g[N];
void dfs(int u, int fa) {
    dep[u] = dep[fa] + 1;
    f[u][0] = fa;
    for (int i = 1; i <= 19; i++)
        f[u][i] = f[f[u][i - 1]][i - 1];
    for (int v : g[u]) {
        if (v == fa)
            continue;
        dfs(v, u);
    }
}
int lca(int x, int y) {
    if (dep[x] < dep[y])
        swap(x, y);
    for (int i = 19; i >= 0; i--) {
        if (dep[f[x][i]] >= dep[y])
            x = f[x][i];
    }
    if (x == y)
        return x;
    for (int i = 19; i >= 0; i--)
        if (f[x][i] != f[y][i])
            x = f[x][i], y = f[y][i];
    return f[x][0];
}
signed main() {
    freopen("query.in", "r", stdin);
    freopen("query.out", "w", stdout);
    IOS;
    cin >> n;
    for (int i = 1, u, v; i < n; i++) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 0);
    cin >> q;
    for (int i = 1; i <= n; i++) {
        qq[i][i] = i;
        for (int j = i + 1; j <= n; j++)
            qq[i][j] = lca(qq[i][j - 1], j);
    }
    for (int i = 1, l, r, k; i <= q; i++) {
        cin >> l >> r >> k;
        int maxx = 0;
        for (int i = l, j = l + k - 1; j <= r; i++, j++) {
            maxx = max(maxx, dep[qq[i][j]]);
        }
        cout << maxx << "\n";
    }
    return 0;
}
/*
1
7 1 2
4 1
*/