#include <bits/stdc++.h>
#define C__ const
using namespace std;
inline void filopen(string path) {
	cin.tie(0)->sync_with_stdio(0);
	freopen((path + ".in").c_str(), "r", stdin);
	freopen((path + ".out").c_str(), "w", stdout);
}
C__ int N_ = 5e5 + 10, L_ = __lg(N_) + 1;
int n, fa[N_][L_], dep[N_];
vector<int> g[N_];
void dfs(int u, int p) {
	fa[u][0] = p, dep[u] = dep[p] + 1;
	for (int i = 1; i < L_; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
	for (int v : g[u])
	if (v != p) dfs(v, u);
}
int lca(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	int d = dep[u] - dep[v];
	for (int i = L_ - 1; ~i; i--) if (d >> i & 1) u = fa[u][i];
	if (u == v) return u;
	for (int i = L_ - 1; ~i; i--)
		if (fa[u][i] != fa[v][i])
			u = fa[u][i], v = fa[v][i];
	return fa[u][0];
} 
namespace Task1 {
	bool check() { return n <= 5000; }
	int a[5010][5010];
	int main() {
		for (int l = 1; l <= n; l++) {
			a[l][l] = dep[l];
			for (int r = l; r <= n; r++) {
				for (int i = l; i <= r; i++) {
					for (int j = i + 1; j <= r; j++) {
						a[l][r] = max(a[l][r], dep[lca(i, j)]);
					}
				}
			}
		}
		int q; cin >> q;
		while (q--) {
			int l, r, k;
			cin >> l >> r >> k;
			int ans = 0;
			for (int i = l; i + k - 1 <= r; i++) {
				for (int j = i + k - 1; j <= r; j++) {
					ans = max(ans, a[i][j]);
				}
			}
			cout << ans << "\n";
		}
		return 0;
	}
}
namespace Task2 {
	int tn, st[N_ << 2];
	int query(int l, int r) {
		int res = 0;
		for (l += tn - 1, r += tn + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
			if (~l&1) res = max(res, st[l ^ 1]);
			if (r & 1) res = max(res, st[r ^ 1]);
		} return res;
	}
	int main() {
		for (tn = 1; tn <= n + 1; tn <<= 1);
		for (int i = 1; i <= n; i++) st[i + tn] = dep[i];
		for (int u = tn - 1; u; u--) st[u] = max(st[u << 1], st[u << 1 | 1]);
		int q; cin >> q;
		while (q--) {
			int l, r, k;
			cin >> l >> r >> k;
			if (k == 1) {
				cout << query(l, r) << "\n";
			} else {
				int ans = 0;
				for (int i = l; i + k - 1 <= r; i++) {
					for (int j = i + k - 1; j <= r; j++) {
						for (int x = i; x <= j; x++) {
							for (int y = x + 1; y <= j; y++) {
								ans = max(ans, dep[lca(x, y)]);
							}
						}
					}
				}
				cout << ans << "\n";
			}
		}
		return 0;
	}
}
int main() {
	filopen("query");
	cin >> n;
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	} dfs(1, 0);
	if (Task1::check()) return Task1::main(); 
	else Task2::main();
	return 0;
}
