#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n,q,lc[5005][5005],dep[N],p[N][25],lg[N];
vector <int> e[N];
void dfs(int k,int fa){
	dep[k] = dep[fa] + 1;
	p[k][0] = fa;
	for(int i = 1; i <= 20; i++){
		p[k][i] = p[p[k][i - 1]][i - 1];
	}
	for(int i = 0; i < e[k].size(); i++){
		if(e[k][i] != fa){
			dfs(e[k][i],k);
		}
	}
}
int lca(int a,int b){
	if(dep[a] < dep[b]) swap(a,b);
	while(dep[a] > dep[b]){
		int f = lg[dep[a] - dep[b]];
		a = p[a][f];
	}
	if(a == b) return a;
	for(int i = 20; i >= 0; i--){
		if(p[a][i] != p[b][i]){
			a = p[a][i];
			b = p[b][i];
		}
	}
	return p[a][0];
}
void subtask1(){
	for(int i = 1; i <= n; i++) lc[i][i] = i;
	for(int l = 2; l <= n; l++){
		for(int i = 1; i <= n - l + 1; i++){
			int j = i + l - 1;
			lc[i][j] = lca(lc[i][j - 1],j);
		}
	}
	while(q--){
		int l,r,k;
		cin >> l >> r >> k;
		int ans = 1;
		for(int i = l; i <= r - k + 1; i++) ans = max(ans,dep[lc[i][i + k - 1]]);
		cout << ans << '\n'; 
	}
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
	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,0);
	cin >> q;
	subtask1();
	return 0;
}
