#include<bits/stdc++.h>
const int N=5e5+3;
using namespace std;
int n;
vector<int>e[N];
namespace Thousand{
	const int M=5001;
	int f[M][14],dep[M],ans[M][M];
	int LCA(int x,int y){
		if(dep[x]<dep[y])swap(x,y);
		for(int i=13;~i;--i){
			if(dep[f[x][i]]>=dep[y])x=f[x][i];
			if(x==y)return y;
		}
		for(int i=13;~i;--i){
			if(f[x][i]!=f[y][i])
				x=f[x][i],y=f[y][i];
		} return f[x][0];
	}
	void dfs(int u,int F){
		dep[u]=dep[F]+1;
		f[u][0]=F;
		for(int i=1;i<=13;++i)
			f[u][i]=f[f[u][i-1]][i-1];
		for(int v:e[u]){
			if(v==F)continue;
			dfs(v,u);
		}
	}
	void prework(){
		dfs(1,0);
		for(int i=1;i<=n;++i){
			int L=i;
			for(int j=i;j<=n;++j){
				L=LCA(L,j);
				ans[i][j]=dep[L];
			}
		}
	}
	void Solve(){
		prework();
		int Q; cin>>Q;
		while(Q--){
			int l,r,k;
			cin>>l>>r>>k;
			int Ans=0;
			for(int u=l;u<=r;++u){
				int v=u+k-1;
				if(v>r)continue;
				Ans=max(Ans,ans[u][v]);
			}
			cout<<Ans<<'\n';
		}
	}
}
signed 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;i<n;++i){
		int u,v; cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	} if(n<=5000)Thousand::Solve();
	return 0;
}
