#include<bits/stdc++.h>
using namespace std;
const int N = 5e3+10;
int f[N][N];

vector<int>e[N];
int dep[N],fa[N];
void dfs(int u,int faa) {
	dep[u] = dep[faa] + 1; fa[u] = faa;
	for(int v:e[u]) {
		if(v != faa) dfs(v,u);
	}
}
int vis[N];
int lca(int x,int y) {
	if(dep[x] > dep[y]) swap(x,y);
	while(dep[x] < dep[y]) {
		if(vis[y] == 1) return x;
		vis[y] = 1;
		y = fa[y];
	}
	if(x == y) return x;
	while(x != y) x = fa[x], y = fa[y];
	return x;
}
int n,q;
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;i<n;i++) {
		int u,v; cin>>u>>v;
		e[u].push_back(v), e[v].push_back(u);
	}
	dfs(1,1);
	for(int i=1;i<=n;i++) {
		memset(vis,0,sizeof vis);
		f[i][i] = i;
		for(int j=i+1;j<=n;j++) {
			f[i][j] = lca(f[i][j-1],j);
		}
	}
	int q; cin>>q;
	while(q--) {
		int l,r,k,ans=0;
		cin>>l>>r>>k;
		for(int i = l; i+k-1 <= r;i++) {
			ans = max(ans, dep[f[i][i+k-1]]);
		}
		cout << ans << '\n';
	}
	return 0;
}

