#include<bits/stdc++.h>
using namespace std; using ll = long long;
#define For(i, j, k) for ( int i = (j) ; i <= (k) ; i++ )
#define Fol(i, j, k) for ( int i = (j) ; i >= (k) ; i-- )
int n, q, p, u, v, dep[500009], ans[500009];
vector < int > g[500009]; set < pair < int, int > > st[500009];
struct mdfs { int l, r, v; inline mdfs(int l, int r, int v) : l(l), r(r), v(v) {} }; vector < mdfs > mdf;
struct ques { int l, r, k, id; } que[500009];
namespace BIT
{
int c[500009];
inline void init() { fill(c + 1, c + n + 1, 0); }
inline void upd(int p, int v) { for ( ; p <= n ; p += p & -p ) c[p] = max(c[p], v); }
inline int qry(int p) { int v = 0; for ( ; p ; p ^= p & -p ) v = max(v, c[p]); return v; }
}
struct ZKW
{
int m, t[500009 << 2];
inline void init() { m = 2 << __lg(n), fill(t + 1, t + m + m, 0); }
inline void upd(int p, int v) { for ( p += m ; p ; p >>= 1 ) if ( t[p] < v ) t[p] = v; else break; }
inline int qry(int l, int r)
{
int z = 0;
for ( l += m - 1, r += m + 1 ; l ^ r ^ 1 ; l >>= 1, r >>= 1 )
~l & 1 && ( z = max(z, t[l ^ 1]) ), r & 1 && ( z = max(z, t[r ^ 1] ) );
return z;
}
} tl, tr;
inline void ins(int u, int l, int r, bool flg = false)
{
auto it = st[u].lower_bound(make_pair(l, r));
if ( it != st[u].end() && it -> first == r + 1 ) r = it -> second, it = st[u].erase(it), flg = true;
if ( it != st[u].begin() ) { auto it2 = prev(it); if ( it2 -> second == l - 1 ) l = it2 -> first, st[u].erase(it2), flg = true; }
st[u].emplace(l, r); if ( flg ) mdf.emplace_back(l, r, dep[u]);
}
inline void dfs(int u, int fa = 0)
{
dep[u] = dep[fa] + 1, ins(u, u, u, true);
for ( int i : g[u] ) if ( i != fa )
{
dfs(i, u);
if ( (int)st[u].size() < (int)st[i].size() ) swap(st[u], st[i]);
for ( auto _ : st[i] ) ins(u, _.first, _.second);
st[i].clear();
}
}
int main()
{
freopen("query.in", "r", stdin), freopen("query.out", "w", stdout);
cin.tie(nullptr) -> sync_with_stdio(false);
cin >> n, BIT::init(), tl.init(), tr.init();
For(i, 2, n) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
dfs(1), cin >> q;
For(i, 1, q) cin >> que[i].l >> que[i].r >> que[i].k, que[i].k--, que[i].id = i;
sort(mdf.begin(), mdf.end(), [&](const mdfs &x, const mdfs &y) { return x.r > y.r; });
sort(que + 1, que + q + 1, [&](const ques &x, const ques &y) { return x.r > y.r; });
For(i, 1, q)
{
for ( ; p != (int)mdf.size() && mdf[p].r >= que[i].r ; p++ ) BIT::upd(mdf[p].l, mdf[p].v);
ans[que[i].id] = BIT::qry(que[i].l);
}
sort(mdf.begin(), mdf.end(), [&](const mdfs &x, const mdfs &y) { return x.r - x.l > y.r - y.l; });
sort(que + 1, que + q + 1, [&](const ques &x, const ques &y) { return x.k > y.k; }), p = 0;
For(i, 1, q)
{
for ( ; p != (int)mdf.size() && mdf[p].r - mdf[p].l >= que[i].k ; p++ )
tl.upd(mdf[p].l, mdf[p].v), tr.upd(mdf[p].r, mdf[p].v);
ans[que[i].id] = max(ans[que[i].id], max(tl.qry(que[i].l, que[i].r - que[i].k), tr.qry(que[i].l + que[i].k, que[i].r)));
}
For(i, 1, q) cout << ans[i] << '\n';
return 0;
}