#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int N = 5005;
int lca[N][N], n, q, ld[N], rd[N], dfn[N], ans[N][N], dep[N];
vector <int> e[N];
void dfs(int x, int pfa){
	dfn[++dfn[0]] = x, ld[x] = dfn[0], dep[x] = dep[pfa] + 1, lca[x][x] = x; 
	for(int y : e[x]){
		if(y == pfa)
			continue;
		dfs(y, x);
		for(int i = ld[x]; i < ld[y]; i++)
			for(int j = ld[y]; j <= rd[y]; j++)
				lca[dfn[i]][dfn[j]] = lca[dfn[j]][dfn[i]] = x;
	}
	rd[x] = dfn[0];
}
int main(){
	freopen("query.in", "r", stdin);
	freopen("query.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for(int i = 1, u, v; i < n; i++){
		cin >> u >> v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs(1, 0);
	for(int l = 1; l <= n; l++){
		ans[l][l] = l;
		for(int r = l + 1; r <= n; r++)
			ans[l][r] = lca[ans[l][r - 1]][r];
	}
	cin >> q;
	while(q--){
		int l, r, k;
		cin >> l >> r >> k;
		int maxn = 0;
		for(int x = l, y = l + k - 1; y <= r; x++, y++)
			maxn = max(maxn, dep[ans[x][y]]);
		cout << maxn << "\n";
	}
	
} 
