#include <bits/stdc++.h>
typedef long long LL;
typedef std::pair<int, int> pii;
#define fi first
#define se second
#define MP std::make_pair
bool Mst;
char buf[1 << 20], *p1, *p2;
#define gc() ((p1 == p2) && ((p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin)), (p1 == p2)) ? 0 : *p1++)
int read()
{
int s = 0, f = 1; char c = gc();
for (; !isdigit(c); c = gc()) f ^= (c == '-');
for (; isdigit(c); c = gc()) s = s * 10 + (c ^ 48);
return f ? s : -s;
}
template<typename T>
void write(T x, char end = '\n')
{
if (x < 0) x = -x, putchar('-');
static int d[100], cur = 0;
do { d[++cur] = x % 10; } while (x /= 10);
while (cur) putchar('0' + d[cur--]);
putchar(end);
}
template<typename T>
void Fmax(T &x, T y){ if (x < y) x = y; }
template<typename T>
void Fmin(T &x, T y){ if (y < x) x = y; }
const int MOD = 1e9 + 7;
int fplus(int x, int y){ return x + y >= MOD ? x + y - MOD : x + y; }
int fminus(int x, int y){ return x >= y ? x - y : x + MOD - y; }
void Fplus(int &x, int y){ x = fplus(x, y); }
void Fminus(int &x, int y){ x = fminus(x, y); }
int fpow(int x, int y = MOD - 2)
{
int res = 1;
for (; y; y >>= 1, x = (LL)x * x % MOD)
if (y & 1) res = (LL)res * x % MOD;
return res;
}
struct Tuple
{
int x, y, z;
Tuple(){}
Tuple(int _x, int _y, int _z)
: x(_x), y(_y), z(_z){}
} ;
const int MAXN = 500005;
int n, Q, siz[MAXN], dep[MAXN], fa[MAXN], son[MAXN], top[MAXN];
int ans[MAXN];
std::vector<int> e[MAXN];
void setup(int x, int fat)
{
fa[x] = fat, dep[x] = dep[fat] + 1;
siz[x] = 1, son[x] = -1;
for (int y : e[x])
{
if (y == fat) continue;
setup(y, x), siz[x] += siz[y];
if (son[x] == -1 || siz[son[x]] < siz[y]) son[x] = y;
}
}
void cut(int x, int t)
{
top[x] = t;
if (~son[x]) cut(son[x], t);
for (int y : e[x]) if (y != fa[x] && y != son[x]) cut(y, y);
}
int getLca(int x, int y)
{
for (; top[x] != top[y]; x = fa[top[x]])
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
return dep[x] < dep[y] ? x : y;
}
Tuple seg[MAXN << 1];
int m = 0;
void init_tree()
{
n = read();
for (int i = 1; i < n; i++)
{
int u = read(), v = read();
e[u].push_back(v), e[v].push_back(u);
}
setup(1, 0), cut(1, 1);
static pii st[MAXN]; int tp;
st[tp = 1] = MP(1, 1);
for (int i = 2; i <= n; i++)
{
int lca = getLca(i, st[tp].fi);
if (lca == st[tp].fi) { st[++tp] = MP(i, i); continue; }
int low = n + 1;
while (dep[st[tp].fi] > dep[lca])
{
seg[++m] = Tuple(st[tp].se, i - 1, dep[st[tp].fi]);
Fmin(low, st[tp--].se);
}
if (st[tp].fi != lca) st[++tp] = MP(lca, low);
if (st[tp].fi != i) st[++tp] = MP(i, i);
}
for (; tp; tp--) seg[++m] = Tuple(st[tp].se, n, dep[st[tp].fi]);
}
std::vector<Tuple> rec[MAXN];
std::vector<Tuple> q[MAXN];
Tuple qu[MAXN];
void init_query()
{
Q = read();
for (int i = 1; i <= Q; i++)
qu[i].x = read(), qu[i].y = read(), qu[i].z = read();
}
namespace SGT
{
#define ls (x << 1)
#define rs (x << 1 | 1)
int sgt[MAXN << 2], N;
void build(int n)
{
for (N = 1; N < n; N <<= 1) ;
memset(sgt, 0, (N * 2) << 2);
}
void modify(int x, int v)
{
Fmax(sgt[x += N - 1], v);
for (x >>= 1; x; x >>= 1)
sgt[x] = std::max(sgt[ls], sgt[rs]);
}
int query(int l, int r)
{
int res = std::max(sgt[l += N - 1], sgt[r += N - 1]);
if (l == r) return res;
for (; l ^ r ^ 1; l >>= 1, r >>= 1)
{
if (!(l & 1)) Fmax(res, sgt[l ^ 1]);
if (r & 1) Fmax(res, sgt[r ^ 1]);
}
return res;
}
#undef ls
#undef rs
}
void calc()
{
for (int i = 1; i <= m; i++)
rec[seg[i].y - seg[i].x + 1].push_back(seg[i]);
for (int i = 1; i <= Q; i++)
q[qu[i].z].emplace_back(qu[i].x, qu[i].y, i);
SGT::build(n);
for (int i = n; i; i--)
{
for (Tuple t : rec[i]) SGT::modify(t.x, t.z);
for (Tuple t : q[i]) Fmax(ans[t.z], SGT::query(t.x, t.y - i + 1));
}
SGT::build(n);
for (int i = n; i; i--)
{
for (Tuple t : rec[i]) SGT::modify(t.y, t.z);
for (Tuple t : q[i]) Fmax(ans[t.z], SGT::query(t.x + i - 1, t.y));
}
for (int i = 1; i <= n; i++) rec[i].clear(), q[i].clear();
for (int i = 1; i <= m; i++)
rec[seg[i].x].emplace_back(seg[i].y, seg[i].z, 0);
for (int i = 1; i <= Q; i++)
q[qu[i].x].emplace_back(qu[i].y, i, 0);
SGT::build(n);
for (int i = 1; i <= n; i++)
{
for (Tuple t : rec[i]) SGT::modify(t.x, t.y);
for (Tuple t : q[i]) Fmax(ans[t.y], SGT::query(t.x, n));
}
}
bool Med;
int main()
{
freopen("query.in", "r", stdin);
freopen("query.out", "w", stdout);
init_tree();
init_query();
calc();
for (int i = 1; i <= Q; i++) write(ans[i]);
return 0;
}