#include <cstdio>
#include <vector>
#include <algorithm>
#define fi first
#define se second
#define lc (p << 1)
#define rc (p << 1 | 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename T = int>
T read() {
char c = getchar();
int x = 0;
bool f = 0;
while (c < 48 or c > 57)
f |= (c == '-'), c = getchar();
do
x = (x * 10) + (c ^ 48), c = getchar();
while (c >= 48 and c <= 57);
return f ? -x : x;
}
const int N = 500003;
const int Lg = 19;
typedef pair<int, int> pii;
int n, q;
int hd[N], ver[N << 1], nxt[N << 1], tot;
void add(int u, int v) {
nxt[++tot] = hd[u], hd[u] = tot, ver[tot] = v;
}
vector<int> vec[N];
int mn[Lg][N], f[Lg][N], g[Lg][N], a[N];
int dfn[N], num, de[N];
int ql[N], qr[N], res[N];
inline int cmpd(int x, int y) {
return dfn[x] < dfn[y] ? x : y;
}
inline int cmp(int x, int y) {
return a[x] < a[y] ? x : y;
}
inline int lca(int u, int v) {
if (u == v)
return u;
if ((u = dfn[u]) > (v = dfn[v]))
swap(u, v);
int k = __lg(v - u++);
return cmpd(mn[k][u], mn[k][v - (1 << k) + 1]);
}
inline int qry(int l, int r) {
int k = __lg(r - l + 1);
return cmp(f[k][l], f[k][r - (1 << k) + 1]);
}
inline int qmx(int l, int r) {
int k = __lg(r - l + 1);
return max(g[k][l], g[k][r - (1 << k) + 1]);
}
void dfs(int u, int fa) {
mn[0][dfn[u] = ++num] = fa;
for (int i = hd[u]; i; i = nxt[i]) {
int v = ver[i];
if (v == fa)
continue;
de[v] = de[u] + 1;
dfs(v, u);
}
}
inline void chmx(int &x, int v) {
if (x < v)
x = v;
}
vector<pii> ins[N], del[N];
int tr[N];
int sg[N << 2], lim[N << 2];
void segins(int x, int y, int v, int p = 1, int l = 1, int r = n) {
chmx(lim[p], y);
chmx(sg[p], v);
if (l == r)
return;
int mid = (l + r) >> 1;
if (x <= mid)
segins(x, y, v, lc, l, mid);
else
segins(x, y, v, rc, mid + 1, r);
}
void segdel(int x, int p = 1, int l = 1, int r = n) {
if (l == r) {
sg[p] = lim[p] = 0;
return;
}
int mid = (l + r) >> 1;
if (x <= mid)
segdel(x, lc, l, mid);
else
segdel(x, rc, mid + 1, r);
sg[p] = max(sg[lc], sg[rc]);
lim[p] = max(lim[lc], lim[rc]);
}
int query(int x, int y, int p = 1, int l = 1, int r = n) {
if (x <= l and lim[p] <= y)
return sg[p];
if (l == r)
return 0;
int mid = (l + r) >> 1;
if (x > mid)
return query(x, y, rc, mid + 1, r);
if (lim[lc] > y)
return query(x, y, lc, l, mid);
return max(query(x, y, lc, l, mid), query(x, y, rc, mid + 1, r));
}
int main() {
freopen("query.in", "r", stdin);
freopen("query.out", "w", stdout);
n = read();
for (int i = 1; i < n; ++i) {
int u = read(), v = read();
add(u, v), add(v, u);
}
de[1] = 1;
dfs(1, 0);
for (int i = 1; i <= n; ++i)
g[0][i] = de[i];
for (int t = 1; t < Lg; ++t)
for (int i = 1; i + (1 << t) - 1 <= n; ++i) {
mn[t][i] = cmpd(mn[t - 1][i], mn[t - 1][i + (1 << (t - 1))]);
g[t][i] = max(g[t - 1][i], g[t - 1][i + (1 << (t - 1))]);
}
for (int i = 1; i < n; ++i)
a[i] = de[lca(i, i + 1)], f[0][i] = i;
for (int t = 1; t < Lg; ++t)
for (int i = 1; i + (1 << t) - 1 < n; ++i)
f[t][i] = cmp(f[t - 1][i], f[t - 1][i + (1 << (t - 1))]);
q = read();
for (int i = 1; i <= q; ++i) {
ql[i] = read();
qr[i] = read();
int k = read() - 1;
if (!k)
res[i] = qmx(ql[i], qr[i]);
else {
--qr[i];
res[i] = max(a[qry(ql[i], ql[i] + k - 1)], a[qry(qr[i] - k + 1, qr[i])]);
vec[k].emplace_back(i);
}
}
ins[n - 1].emplace_back(1, n - 1);
for (int k = n - 1; k; --k) {
for (pii cur : del[k])
segdel(cur.fi);
for (pii cur : ins[k]) {
int o = qry(cur.fi, cur.se);
segins(cur.fi, cur.se, a[o]);
int now = 0;
if (cur.fi < o) {
chmx(now, o - cur.fi);
ins[o - cur.fi].emplace_back(cur.fi, o - 1);
}
if (cur.se > o) {
chmx(now, cur.se - o);
ins[cur.se - o].emplace_back(o + 1, cur.se);
}
if (now)
del[now].emplace_back(cur);
}
for (int x : vec[k]) {
int l = ql[x], r = qr[x];
chmx(res[x], query(l, r));
}
}
for (int i = 1; i <= q; ++i)
printf("%d\n", res[i]);
return 0;
}