#include <bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
#define fd(x , y , z) for(int x = y ; x >= z ; x --)
using namespace std;
const int N = 5e5 + 5;
int n , f[N][25] , dep[N] , g[5005][5005] , rt[N] , max_dep;
vector<int> e[N];
vector<int> d[N];
struct Tr {
    int lp , rp , s;
} tr[N * 20 * 4];
void dfs (int x , int fa) {
    dep[x] = dep[fa] + 1;
    f[x][0] = fa;
    fu (i , 1 , 20) f[x][i] = f[f[x][i - 1]][i - 1];
    for (auto i : e[x]) {
        if (i == fa) continue;
        dfs (i , x);
    }
}
int Lca (int x , int y) {
    if (dep[x] < dep[y]) swap (x , y);
    fd (i , 20 , 0)
        if (dep[f[x][i]] >= dep[y]) x = f[x][i];
    if (x == y) return x;
    fd (i , 20 , 0) {
        if (f[x][i] != f[y][i]) x = f[x][i] , y = f[y][i];
    }
    return f[x][0];
}
int pos;
void pu (int p) { tr[p].s = tr[tr[p].lp].s + tr[tr[p].rp].s; }
void chg (int &p , int l , int r , int x) {
    p = ++pos;
    if (l == r) tr[p].s = 1;
    else {
        int mid = l + r >> 1;
        if (x <= mid) chg (tr[p].lp , l , mid , x);
        else chg (tr[p].rp , mid + 1 , r , x);
        pu (p);
    }
}
void merge (int &x , int y , int l , int r) {
    if (x == 0 || y == 0) x += y;
    else if (l == r) tr[x].s += tr[y].s;
    else {
        int mid = l + r >> 1;
        merge (tr[x].lp , tr[y].lp , l , mid);
        merge (tr[x].rp , tr[y].rp , mid + 1 , r);
        pu (x);
    }
}
void dfs2 (int x , int fa) {
    dep[x] = dep[fa] + 1;
    max_dep = max (max_dep , dep[x]);
    d[dep[x]].push_back(x);
    chg (rt[x] , 1 , n , x);
    for (auto i : e[x]) {
        if (i == fa) continue;
        dfs2 (i , x);
        merge (rt[x] , rt[i] , 1 , n);
    }
}
int qry (int p , int l , int r , int L , int R) {
    if (p == 0) return 0;
    if (L <= l && r <= R) return tr[p].s;
    else {
        int mid = l + r >> 1 , ans = 0;
        if (L <= mid) ans = qry (tr[p].lp , l , mid , L , R);
        if (mid < R) ans += qry (tr[p].rp , mid + 1 , r , L , R);
        return ans;
    }
}
bool ck (int mid , int l , int r , int k) {
    for (auto i : d[mid]) {
        if (tr[rt[i]].s < k) continue;
        // cout << qry (rt[i] , 1 , n , l , r) << "\n";
        // exit (0);
        if (qry (rt[i] , 1 , n , l , r) >= k) return 1;
    }
    return 0;
}
int main () {
    // freopen ("a.in" , "r" , stdin);
    freopen ("query.in" , "r" , stdin);
    freopen ("query.out" , "w" , stdout);
    scanf ("%d" , &n);
    fu (i , 1 , n - 1) {
        int u , v;
        scanf ("%d%d" , &u , &v);
        e[u].push_back(v) , e[v].push_back(u);
    }
    int T;
    if (n <= 5000) {
        dfs (1 , 0);
        fu (l , 1 , n) {
            g[l][l] = l;
            fu (r , l + 1 , n) {
                g[l][r] = Lca (g[l][r - 1] , r);
            }
        }
        scanf ("%d" , &T);
        int ans;
        while (T --) {
            int l , r , k;
            scanf ("%d%d%d" , &l , &r , &k);
            ans = dep[g[l][l + k - 1]];
            fu (i , l + 1 , r - k + 1) {
                if (ans < dep[g[i][i + k - 1]]) {
                    ans = dep[g[i][i + k - 1]];
                }
            }
            printf ("%d\n" , ans);
        }
        return 0;
    }
    dfs2 (1 , 0);
    scanf ("%d" , &T);
    int l , r , ans , mid;
    while (T --) {
        int L , R , k;
        scanf ("%d%d%d" , &L , &R , &k);
        l = 1 , r = max_dep;
        while (l <= r) {
            mid = l + r >> 1;
            if (ck (mid , L , R , k)) l = mid + 1 , ans = mid;
            else r = mid - 1;
        }
        printf ("%d\n" , ans);
    }
    return 0;
}