#include<bits/stdc++.h>
using namespace std;
int n,u,v,q,l,r,k,dep[500005],id[500005],sz[500005],nw[500005],f[5005][5005],g[5005][5005],cnt,ff[500005][25];
vector<int>ve[500005];
class Segment_Tree{
public:
	struct tree{
		int l,r,maxx,minx;
	}tr[2000005];
	void pushup(int u){
		tr[u].maxx=max(tr[u<<1].maxx,tr[u<<1|1].maxx);
		tr[u].minx=min(tr[u<<1].minx,tr[u<<1|1].minx);
	}
	void build(int u,int l,int r){
		tr[u]={l,r,0,0};
		if(l==r){
			tr[u].minx=tr[u].maxx=id[l];
			return;
		}
		int mid=l+r>>1;
		build(u<<1,l,mid),build(u<<1|1,mid+1,r);
		pushup(u);
	}
	int query1(int u,int l,int r){
		if(tr[u].l>=l&&tr[u].r<=r)return tr[u].maxx;
		int mid=tr[u].l+tr[u].r>>1,res=0;
		if(l<=mid)res=max(res,query1(u<<1,l,r));
		if(r>mid)res=max(res,query1(u<<1|1,l,r));
		return res;
	}
	int query2(int u,int l,int r){
		if(tr[u].l>=l&&tr[u].r<=r)return tr[u].minx;
		int mid=tr[u].l+tr[u].r>>1,res=1e9;
		if(l<=mid)res=min(res,query2(u<<1,l,r));
		if(r>mid)res=min(res,query2(u<<1|1,l,r));
		return res;
	}
}sgt;
void dfs(int u,int fa){
	id[u]=++cnt,nw[id[u]]=u,sz[u]=1,dep[u]=dep[fa]+1;
	ff[u][0]=fa;
	for(int v:ve[u]){
		if(v==fa)continue;
		dfs(v,u),sz[u]+=sz[v];
	}
}
void dfs2(int u,int fa){
	for(int i=id[u];i<id[u]+sz[u];i++)f[u][nw[i]]=f[nw[i]][u]=u;
	for(int v:ve[u])if(v!=fa){
		for(int w:ve[u])if(w!=fa){
			for(int i=id[v];i<id[v]+sz[v];i++){
				for(int j=id[w];j<id[w]+sz[w];j++)f[nw[i]][nw[j]]=u;
			}
		}
	}
	for(int v:ve[u]){
		if(v==fa)continue;
		dfs2(v,u);
	}
}
int lca(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	for(int i=20;~i;i--)if(dep[ff[u][i]]>=dep[v])u=ff[u][i];
	if(u==v)return u;
	for(int i=20;~i;i--)if(ff[u][i]!=ff[v][i])u=ff[u][i],v=ff[v][i];
	return ff[u][0];
}
signed 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++)cin>>u>>v,ve[u].emplace_back(v),ve[v].emplace_back(u);
	if(n<=5000){
		dfs(1,0),dfs2(1,0);
		for(int i=1;i<=n;i++)for(int j=i;j<=n;j++){
			if(i==j)g[i][j]=i;
			else g[i][j]=f[g[i][j-1]][j];
		}
		cin>>q;
		while(q--){
			cin>>l>>r>>k;
			int ans=0;
			for(int i=l;i<=r-k+1;i++)ans=max(ans,dep[g[i][i+k-1]]);
			cout<<ans<<"\n";
		}
		return 0;
	}
	dfs(1,0),sgt.build(1,1,n);
	for(int i=1;i<=20;i++)for(int j=1;j<=n;j++)ff[j][i]=ff[ff[j][i-1]][i-1];
	cin>>q;
	while(q--){
		cin>>l>>r>>k;
		if(k==r-l+1){
			int aa=sgt.query1(1,l,r),bb=sgt.query2(1,l,r);
			aa=nw[aa],bb=nw[bb];
			cout<<dep[lca(aa,bb)]<<"\n";
		}
	}
	return 0;
}
