#include <bits/stdc++.h>
using namespace std;
int n,u,v,q,dep[500005],s[5005][5005],minn[5005][20],f[5005][5005];
vector<int> h[500005],g[5005],dp[5005];
void dfs(int u,int fa){
	dep[u]=dep[fa]+1;
	dp[dep[u]].push_back(u);
	g[u].push_back(u);
	for(int i=0;i<h[u].size();i++){
		int v=h[u][i];
		if(v==fa)continue;
		dfs(v,u);
		for(int j=0;j<g[v].size();j++){
			g[u].push_back(g[v][j]);
		}
	}
}
void init(){
	for(int i=1;i<=n;i++){
		minn[i][0]=dep[i];
	}
	for(int j=1;j<=18;j++){
		for(int i=1;i+(1<<j)<=n;i++){
			minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
		}
	}
}
bool check(int x,int l,int r){
	for(int i=0;i<dp[x].size();i++){
		int u=dp[x][i];
		if(s[u][r]-s[u][l-1]==r-l+1)return 1;
	}
	return 0;
}
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=1;i<n;i++){
		cin>>u>>v;
		h[u].push_back(v);
		h[v].push_back(u);
	}
	dfs(1,0);
	init();
	for(int i=1;i<=n;i++){
		sort(g[i].begin(),g[i].end());
		int pos=0;
		for(int j=1;j<=n;j++){
			s[i][j]=s[i][j-1];
			if(pos==g[i].size())continue;
			if(j==g[i][pos]){
				s[i][j]++;
				pos++;
			}
		}
	}
	for(int l=1;l<=n;l++){
		for(int r=l;r<=n;r++){
			int k=log2(r-l+1);
			int mindep=min(minn[l][k],minn[r-(1<<k)+1][k]);
			int ll=1,rr=mindep,pos,mid;
			while(ll<=rr){
				mid=ll+rr>>1;
				if(check(mid,l,r)){
					ll=mid+1;
					pos=mid;
				}else rr=mid-1;
			}
			f[l][r]=pos;
		}
	}
	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,f[i][i+k-1]);
		}
		cout<<ans<<'\n';
	}
}
