#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5, inf = 2e9;
int st[maxn][21], tot, dfn[maxn], n, q, dep[maxn], nca[5005][5005], lg[maxn];
vector<int> e[maxn];
void srh(int u, int fa) {
	dep[u] = dep[fa] + 1;
	st[++tot][0] = u, dfn[u] = tot;
	for (int i = 0; i < e[u].size(); i++) {
		int v = e[u][i];
		if(v == fa)
			continue;
		srh(v, u);
		st[++tot][0] = u;
 	}
}
void prepare() {
	lg[0] = -1;
	for (int i = 1; i <= tot; i++)
		lg[i] = lg[i >> 1] + 1;
	for (int j = 1; (1 << j) <= tot; j++)
		for (int i = 1; i <= tot; i++) {
			if(dep[st[i][j - 1]] < dep[st[i + (1 << j - 1)][j - 1]])
				st[i][j] = st[i][j - 1];
			else
				st[i][j] = st[i + (1 << j - 1)][j - 1];
		}
}
int lca(int x, int y) {
	x = dfn[x], y = dfn[y];
	if(x > y)
		swap(x, y);
	int k = lg[y - x + 1];
	if(dep[st[x][k]] < dep[st[y - (1 << k) + 1][k]])
		return st[x][k];
	return st[y - (1 << k) + 1][k];
}
void work1() {
	for (int i = 1; i <= n; i++) {
		nca[i][i] = i;
		for (int j = i + 1; j <= n; j++)
			nca[i][j] = lca(nca[i][j - 1], j);
	}
	cin >> q;
	while(q--) {
		int l, r, k;
		cin >> l >> r >> k;
		int ans = 0;
		for (int i = l; i <= r - k + 1; i++)
			ans = max(ans, dep[nca[i][i + k - 1]]);
		cout << ans << endl;
	}
}
void work2() {
	
}
struct Segtree {
	int tr[maxn << 2];
	void pushup(int t) {
		tr[t] = lca(tr[t << 1], tr[t << 1 | 1]);
	}
	void build(int l, int r, int t) {
		if(l == r) {
			tr[t] = l;
			return ;
		}
		int mid = l + r >> 1;
		build(l, mid, t << 1), build(mid + 1, r, t << 1 | 1);
		pushup(t); 
	}
	int query(int l, int r, int x, int y, int t) {
		if(x <= l && r <= y)
			return tr[t];
		int mid = l + r >> 1;
		if(y <= mid)
			return query(l, mid, x, y, t << 1);
		if(mid < x)
			return query(mid + 1, r, x, y, t << 1 | 1);
		return lca(query(l, mid, x, y, t << 1), query(mid + 1, r, x, y, t << 1 | 1));
	}
} tree;
void work3() {
	tree.build(1, n, 1);
	cin >> q;
	while(q--) {
		int l, r, k;
		cin >> l >> r >> k;
		cout << dep[tree.query(1, n, l, r, 1)] << endl; 
	}
}
int main() {
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	cin >> n;
	bool f1 = 1;
	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		e[x].push_back(y);
		e[y].push_back(x); 
	}
	f1 &= (e[1].size() == 1);
	for (int i = 1; i <= n; i++)
		f1 &= (e[i].size() <= 2);
	srh(1, 0);
	prepare();
	if(n <= 5000) 
		work1();
	else if(f1)
		work2();
	else
		work3();
	return 0;
}
