#include <cstdio>
#include <vector>
using namespace std;
vector<int> g[500005];
int dep[500005], fa[500005][20], st[500005][20], lg2[530000];
void dfs(int x) {
    dep[x] = dep[fa[x][0]] + 1;
    for (int i = 1; i < 20; i++)
        fa[x][i] = fa[fa[x][i - 1]][i - 1];
    for (int i: g[x])
        if (i != fa[x][0]) {
            fa[i][0] = x;
            dfs(i);
        }
}
int lca(int x, int y) {
    if (dep[x] < dep[y])
        swap(x, y);
    for (int i = dep[x] - dep[y], j = 0; i; i >>= 1, j++)
        if (i & 1)
            x = fa[x][j];
    if (x == y)
        return x;
    for (int i = 19; i >= 0; i--)
        if (fa[x][i] != fa[y][i]) {
            x = fa[x][i];
            y = fa[y][i];
        }
    return fa[x][0];
}
int main() {
    freopen("query.in", "r", stdin);
    freopen("query.out", "w", stdout);
    for (int i = 2; i <= 500000; i++)
        lg2[i] = lg2[i - 1] + (1 << lg2[i - 1] + 1 == i);
    int n;
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    dfs(1);
    for (int i = 1; i <= n; i++)
        st[i][0] = i;
    for (int j = 1; j < 20; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; i++)
            st[i][j] = lca(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r, k, ans = 0;
        scanf("%d%d%d", &l, &r, &k);
        for (int i = l; i + k - 1 <= r; i++)
            ans = max(ans, dep[lca(st[i][lg2[k]], st[i + k - (1 << lg2[k])][lg2[k]])]);
        printf("%d\n", ans);
    }
    return 0;
}