#include<bits/stdc++.h>
#define FIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define eb emplace_back
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=5e5+5;
const int inf=1e9+7;

int n,m;
vector<int> e[N];

struct nd{
	int l,r,k,id;
}q[N];

int f[N],dep[N],sz[N],gs[N],top[N],dfn[N],mp[N],tot;

void dfs1(int u,int fa){
	f[u]=fa,dep[u]=dep[fa]+1,sz[u]=1;
	for(int v:e[u]){
		if(v==fa) continue;
		dfs1(v,u),sz[u]+=sz[v];
		if(sz[gs[u]]<sz[v]) gs[u]=v;
	} 
}
void dfs2(int u,int t){
	top[u]=t,mp[dfn[u]=++tot]=u;
	if(!gs[u]) return;
	dfs2(gs[u],t);
	for(int v:e[u]) if(v^f[u]&&v^gs[u]) dfs2(v,v);
}
int lca(int x,int y){
	while(top[x]^top[y]) dep[top[x]]>=dep[top[y]]?x=f[top[x]]:y=f[top[y]];
	return dep[x]<dep[y]?x:y; 
}

namespace sub1{
	bool chk(){
		if(n<=5000) return 1;
		for(int i=1;i<=m;++i) if(q[i].k!=q[i].r-q[i].l+1) return 0;
		return 1;
	}
	int lg[N],f[2][19][N];
	int qmn(int l,int r){
		int k=lg[r-l+1];
		return min(f[0][k][l],f[0][k][r-(1<<k)+1]);
	}
	int qmx(int l,int r){
		int k=lg[r-l+1];
		return max(f[1][k][l],f[1][k][r-(1<<k)+1]);
	}
	void solve(){
		for(int i=2;i<=n;++i) lg[i]=lg[i>>1]+1;
		for(int i=1;i<=n;++i) f[0][0][i]=f[1][0][i]=dfn[i];

		for(int j=1;j<=lg[n];++j)
			for(int i=1;i+(1<<j)-1<=n;++i)
				f[0][j][i]=min(f[0][j-1][i],f[0][j-1][i+(1<<(j-1))]);
		for(int j=1;j<=lg[n];++j)
			for(int i=1;i+(1<<j)-1<=n;++i)
				f[1][j][i]=max(f[1][j-1][i],f[1][j-1][i+(1<<(j-1))]);
		for(int i=1;i<=m;++i){
			int l=q[i].l,r=q[i].r,k=q[i].k,as=0;
			for(int j=l;j+k-1<=r;++j){
				int x=mp[qmn(j,j+k-1)],y=mp[qmx(j,j+k-1)];
				as=max(as,dep[lca(x,y)]);
			}
			printf("%d\n",as);
		}
	}
}

signed main(){
	FIO("query");
	scanf("%d",&n);
	for(int i=1;i<n;++i){
		int u,v; scanf("%d%d",&u,&v);
		e[u].eb(v),e[v].eb(u);
	}
	scanf("%d",&m);
	for(int i=1;i<=m;++i){
		scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);
		q[i].id=i;
	}
	dfs1(1,0),dfs2(1,1);
	if(sub1::chk()) return sub1::solve(),0;
}