#include<bits/stdc++.h>
#define re register int
#define il inline
using namespace std;
const int N=5e5+10;
int dep[N],T,lc[5010][5010],fa[N][22],u,v,head[N],tot,du[N],st[N][22],lian[N],cnt,st1[N][22];
int n,q,l,r,k,ans;
int llg[N];
bool G=0;
struct Node{
	int to,nxt;
}e[N<<1];
il int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){
		x=x*10+(ch-'0');
		ch=getchar();
	}
	return x;
}
il void Add(int u,int v){
	tot++;
	e[tot].to=v;
	e[tot].nxt=head[u];
	head[u]=tot;
}
il void dfs1(int now,int ff){
	cnt++;
	lian[cnt]=now;
	dep[now]=dep[ff]+1;
	st[now][0]=now;
	st1[now][0]=dep[now];
	for(re i=head[now];i;i=e[i].nxt){
		int To=e[i].to;
		if(To!=ff)
			dfs1(To,now);
	}
}
il void dfs(int now,int ff){
	fa[now][0]=ff;
	dep[now]=dep[ff]+1;
	for(re i=head[now];i;i=e[i].nxt){
		int To=e[i].to;
		if(To!=ff)dfs(To,now);
	}
}
il int LCA(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	for(re i=20;i>=0;i--){
		if(dep[fa[u][i]]>=dep[v])u=fa[u][i];
	}
	if(u==v)return u;
	for(re i=20;i>=0;i--){
		if(fa[u][i]!=fa[v][i]){
			u=fa[u][i];
			v=fa[v][i];
		}
	} 
	return fa[u][0];
}
il int Ask(int l,int r){
	int len=r-l+1;
	if(dep[st[l][llg[len]]]<=dep[st[r-(1<<llg[len])+1][llg[len]]])return st[l][llg[len]];
	else return st[r-(1<<llg[len])+1][llg[len]];
}
il int Askdep(int l,int r){
	int len=r-l+1;
	return max(st1[l][llg[len]],st1[r-(1<<llg[len])+1][llg[len]]);
}
il void getans(int l,int r,int k){
	if(l>r)return;
	int mid=Ask(l,r);
	ans=max(ans,dep[mid]);
	if(Askdep(l,r)<ans)return;
	if(mid-l>=k)getans(l,mid-1,k);
	if(r-mid>=k)getans(mid+1,r,k);
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	for(re i=1;i<n;i++){
		u=read();
		v=read();
		Add(u,v);
		Add(v,u);
		du[u]++;
		du[v]++;
		if(du[u]>2||du[v]>2)G=1;
	}
	if(!G&&du[1]==1){
		dfs1(1,0);
		for(re i=2;i<=n;i++)llg[i]=llg[i/2]+1;
		for(re i=1;i<=20;i++){
			for(re j=1;j+(1<<i)-1<=n;j++){
				if(dep[st[j][i-1]]<dep[st[j+(1<<(i-1))][i-1]])st[j][i]=st[j][i-1];
				else st[j][i]=st[j+(1<<(i-1))][i-1];
				st1[j][i]=max(st1[j+(1<<(i-1))][i-1],st1[j][i-1]);
			}
		}
		q=read();
		while(q--){
			l=read();r=read();k=read();
			ans=0;
			getans(l,r,k);
			printf("%d\n",ans);
		}
	}
	else{
		dfs(1,0);
		for(re i=1;i<=20;i++){
			for(re j=1;j<=n;j++)
				fa[j][i]=fa[fa[j][i-1]][i-1];
		}
		for(re i=1;i<=n;i++){
			lc[i][i]=dep[i];
			int nw=i;
			for(re j=i+1;j<=n;j++){
				nw=LCA(nw,j);
				lc[i][j]=dep[nw];
			}
		}
		q=read();
		while(q--){
			l=read();
			r=read();
			k=read();
			ans=0;
			for(re i=l;i+k-1<=r;i++)
				ans=max(ans,lc[i][i+k-1]);
			printf("%d\n",ans);
		}
	}
	return 0;
}
