#include<bits/stdc++.h>
#define ll long long
#define R register
using namespace std;
const int N=5e5+5;
struct edge{int u,v;}e[N<<2];
int head[N],tot;
inline void add(int u,int v){e[++tot]={v,head[u]};head[u]=tot;}
int n,cur,id[N],son[N],top[N],fa[N],siz[N],dep[N],q;
namespace LCA{
	inline void dfs1(int u,int f){
		fa[u]=f;dep[u]=dep[f]+1;siz[u]=1;
		for(R int i=head[u];i;i=e[i].v){
			int v=e[i].u;
			if(v==f)continue;
			dfs1(v,u);
			siz[u]+=siz[v];
			if(siz[son[u]]<siz[v])son[u]=v;
		}
	}
	inline void dfs2(int u,int tp){
		top[u]=tp;id[u]=++cur;
		if(!son[u])return;
		dfs2(son[u],tp);
		for(R int i=head[u];i;i=e[i].v){
			int v=e[i].u;
			if(v==fa[u]||v==son[u])continue;
			dfs2(v,v);
		}
	}
	inline int lca(int u,int v){
		while(top[u]!=top[v]){
			if(dep[top[u]]<dep[top[v]])swap(u,v);u=fa[top[u]];
		}
		if(dep[u]>dep[v])swap(u,v);
		return u;
	}
}
int resu[5005][5005];
struct SEG{
	int tr[N<<2];
	inline void pushup(int p){tr[p]=LCA::lca(tr[p<<1],tr[p<<1|1]);}
	inline void build(int l,int r,int p){
		if(l==r){
			tr[p]=l;
			return;
		}
		int mid=l+r>>1;
		build(l,mid,p<<1);build(mid+1,r,p<<1|1);
		pushup(p);
	}
	inline int qry(int l,int r,int s,int t,int p){
		if(l<=s&&t<=r)return tr[p];
		int mid=s+t>>1;
		if(mid>=l&&mid<r)return LCA::lca(qry(l,r,s,mid,p<<1),qry(l,r,mid+1,t,p<<1|1));
		else if(mid>=l)return qry(l,r,s,mid,p<<1);
		return qry(l,r,mid+1,t,p<<1|1); 
	}
}S;
int 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(R int i=1;i<n;++i){
		int u,v;cin>>u>>v;add(u,v);add(v,u);
	}
	cin>>q;
	LCA::dfs1(1,0);LCA::dfs2(1,1);
	if(n<=5000&&q<=5000){
		for(R int i=1;i<=n;++i){
			int st=i;resu[i][i]=st;
			for(R int j=i+1;j<=n;++j){
				st=LCA::lca(st,j);resu[i][j]=st;
			}
		}
		while(q--){
			int l,r,k;cin>>l>>r>>k;
			int sum=-1e9;
			for(R int i=l;i+k-1<=r;++i){
				if(dep[resu[i][i+k-1]]>sum)sum=dep[resu[i][i+k-1]];
			}
			cout<<sum<<'\n';
		}
	}else{
		S.build(1,n,1);
		while(q--){
			int l,r,k;cin>>l>>r>>k;
			cout<<dep[S.qry(l,r,1,n,1)]<<'\n';
		}
	}
	return 0;
}
/*
6
5 6
6 1
6 2
2 3
2 4
3
2 5 2
1 4 1
1 6 3

3
4
3
*/

