#include<bits/stdc++.h>
using namespace std;
const int N=5e3+10;
int n,U,V,fa[N],_,l,r,k,lca[N][N],qlca[N][N],dep[N],ans;
vector<int>e[N];
void dfs(int p,int f){
	fa[p]=f;dep[p]=dep[f]+1;
	for(auto v:e[p])if(v!=f)dfs(v,p);
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>U>>V;
		e[U].push_back(V);
		e[V].push_back(U);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			int pi=i,pj=j;
			while(pi!=pj){
				if(dep[pi]==dep[pj])pi=fa[pi];
				if(dep[pi]<dep[pj])pj=fa[pj];
				if(dep[pj]<dep[pi])pi=fa[pi];
			}
			lca[i][j]=pi;
		}
	}
	for(int i=1;i<=n;i++){
		int lc=i;qlca[i][i]=i;
		for(int j=i+1;j<=n;j++){
			lc=lca[lc][j];
			qlca[i][j]=lc;
		}
	}
	cin>>_;
	while(_--){
		cin>>l>>r>>k;
		ans=0;
		for(int len=k;len<=n;len++){
			for(int i=l;i+len-1<=r;i++){
				int j=i+len-1;
				ans=max(ans,dep[qlca[i][j]]);
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}
