#include <bits/stdc++.h>
#define pii pair<int, int>
#define fir first
#define sec second
using namespace std;

const int N = 5e3 + 5;

int n, q;
int lc[N][N], fa[N][20], dep[N];
vector<int> g[N];

void dfs(int u, int f){
	fa[u][0] = f, dep[u] = dep[f] + 1;
	for (auto v : g[u]){
		if (v == f) continue;
		dfs(v, u);
	}
}

int lca(int u, int v){
	if (dep[u] < dep[v]) swap(u, v);
	for (int i = 19; i >= 0; i--){
		if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
	}
	if (u == v) return u;
	for (int i = 19; i >= 0; i--){
		if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
	}
	return fa[u][0];
}

int main(){
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout); 
	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(1, 0);
	for (int j = 1; j <= 19; j++){
		for (int i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
	}
	for (int i = 1; i <= n; i++) lc[i][i] = i;
	for (int len = 2; len <= n; len++){
		for (int l = 1; l + len - 1 <= n; l++){
			int r = l + len - 1;
			lc[l][r] = lca(lc[l][r - 1], r);
		}
	}
	cin >> q;
	while (q--){
		int l, r, k; cin >> l >> r >> k;
		int ans = 0;
		for (int i = l; i + k - 1 <= r; i++) ans = max(ans, dep[lc[i][i + k - 1]]);
		cout << ans << endl;
	}
	return 0;
}

