#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, q, dep[N], f[21][N << 1], lca[21][N], tot, dfn[N], Log[N << 1], maxd;
vector<int> g[N];
void dfs(int u = 1, int fat = 0) {
	f[0][dfn[u] = ++tot] = u;
	dep[u] = dep[fat] + 1;
	for (int v : g[u]) if (v != fat) {
		dfs(v, u);
		f[0][++tot] = u;
	}
}
int Max(int a, int b) {return (dep[a] < dep[b] ? a : b); }
int Min(int a, int b) {return (dep[a] > dep[b] ? a : b); }
void init() {
	for (int i = 2; i <= tot; i++) Log[i] = Log[i >> 1] + 1;
	for (int j = 1; j <= Log[tot]; j++) {
		for (int i = 1; i + (1 << j) - 1 <= tot; i++) {
			f[j][i] = Max(f[j - 1][i], f[j - 1][i + (1 << j - 1)]);
		}
	}
}
int LCA(int x, int y) {
	int l = dfn[x], r = dfn[y];
	if (l > r) l ^= r ^= l ^= r; 
	int k = Log[r - l + 1];
	return Max(f[k][l], f[k][r - (1 << k) + 1]);
}
void init2() {
	for (int i = 1; i <= n; i++) lca[0][i] = i;
	for (int j = 1; j <= Log[n]; j++) {
		for (int i = 1; i + (1 << j) - 1 <= n; i++) {
			lca[j][i] = LCA(lca[j - 1][i], lca[j - 1][i + (1 << j - 1)]);
		}
	}
}
int ask(int l, int r) {
	int k = Log[r - l + 1];
	return LCA(lca[k][l], lca[k][r - (1 << k) + 1]);
}
void baoli() {
	cin >> q;
	for (int l, r, k; q--; ) {
		cin >> l >> r >> k;
		int res = 0;
		for (int j = l; j <= r + 1 - k; j++) res = max(res, dep[ask(j, j + k - 1)]);
		cout << res << '\n';
	}
}
const int M = N * 21;
int pid, cnt[M], ls[M], rs[M], rt[N];
void upd(int x, int& rt, int l = 1, int r = n) {
	if (x > r or x < l) return ;
	pid++, cnt[pid] = cnt[rt], ls[pid] = ls[rt], rs[pid] = rs[rt], rt = pid;
	if (l == r) return cnt[rt]++, void();
	int mid = l + r >> 1;
	upd(x, ls[rt], l, mid), upd(x, rs[rt], mid + 1, r);
	cnt[rt] = cnt[ls[rt]] + cnt[rs[rt]];
}
int kth(int k, int rt1, int rt2, int l = 1, int r = n) {
	if (l == r) return l;
	int mid = l + r >> 1, val = cnt[rs[rt1]] - cnt[rs[rt2]];
	if (val >= k) return kth(k, rs[rt1], rs[rt2], mid + 1, r);
	else return kth(k - val, ls[rt1], ls[rt2], l, mid);
}
mt19937 rnd(time(0));
void solve() {
	for (int i = 1; i <= n; i++) upd(dep[i], rt[i] = rt[i - 1]);
	cin >> q;
	for (int l, r, k; q--; ) {
		cin >> l >> r >> k;
		if (r - l + 1 - k <= 20) {
			int res = 0;
			for (int j = l; j <= r + 1 - k; j++) res = max(res, dep[ask(j, j + k - 1)]);
			cout << res << '\n';
		} else if (maxd == n) cout << kth(k, rt[r], rt[l - 1]) << '\n';
		else {
			int res = 0;
			for (int j = 0; j < 20; j++) {
				int pos = rnd() % (r - l + 2 - k) + l;
				res = max(res, dep[ask(pos, pos + k - 1)]);
			}
			cout << res << '\n';
		}
	}
}
int main() {
	freopen("query.in", "r", stdin); freopen("query.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		g[u].push_back(v), g[v].push_back(u);
	}
	dfs(), init(), init2();
	for (int i = 1; i <= n; i++) maxd = max(maxd, dep[i]);
	if (n <= 5000) baoli(); 
	else solve();
}
