#include<bits/stdc++.h>
using namespace std;
const int N=500005;
inline 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;
}
int n,q,dfn[N],tim,f[N],de[N],wson[N],sz[N],top[N],cnt[N];
int to[2*N],ne[2*N],fi[N],tot;
bool ischain=1;
void link(int x,int y){
	to[++tot]=y; ne[tot] = fi[x]; fi[x]=tot;
}
void dfs1(int x,int fa){
	dfn[x] = ++tim; f[x]=fa; de[x]=de[fa]+1; sz[x]=1;
	for(int i=fi[x];i;i=ne[i]) if(to[i]!=fa){
		dfs1(to[i],x);
		sz[x]+=sz[to[i]];
		if(sz[wson[x]]<sz[to[i]]) wson[x]=to[i];
	}
}
void dfs2(int x,int Top){
	top[x]=Top;
	if(wson[x]) dfs2(wson[x],Top);
	for(int i=fi[x];i;i=ne[i]) if(to[i]!=f[x] && to[i]!=wson[x]){
		dfs2(to[i],to[i]);
	} 
}
int LCA(int x,int y){
	while(top[x]!=top[y]){
		if(de[top[x]]<de[top[y]]) swap(x,y);
		x = f[top[x]];
	}
	if(de[x]<de[y]) return x;
	else return y;
}
namespace testA{
	void work(){
		q=read();
		int l,r,k;
		while(q--){
			l=read(),r=read(),k=read();
			int ans=0;
			for(int i=l;i+k-1<=r;i++){
				int lca = LCA(i,i+1);
				if(k==1) lca = i;
				for(int j=i+2;j<=i+k-1;j++){
					lca = LCA(lca,j);
				}
				ans = max(ans,de[lca]);
			}
			printf("%d\n",ans);
		}
	}
}
struct tree{
	int maxid,ma,minid,mi;
}t[4*N];
tree push_up(tree x,tree y){
	tree an;
	if(x.ma>y.ma) an.ma=x.ma, an.maxid=x.maxid;
	else  an.ma=y.ma, an.maxid=y.maxid;
	if(x.mi<y.mi) an.mi=x.mi, an.minid=x.minid;
	else an.mi=y.mi, an.minid=y.minid;
	return an;
}
void build(int x,int l,int r){
	if(l==r){
		t[x].maxid=l; t[x].ma=dfn[l];
		t[x].minid=l; t[x].mi=dfn[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
	t[x] = push_up(t[x<<1],t[x<<1|1]);
}
tree query(int x,int l,int r,int L,int R){
	if(l>=L && r<=R){
		return t[x];
	}
	int mid=(l+r)>>1;
	if(mid<L) return query(x<<1|1,mid+1,r,L,R);
	if(mid>=R) return query(x<<1,l,mid,L,R);
	return push_up(query(x<<1,l,mid,L,R),query(x<<1|1,mid+1,r,L,R));  
}
namespace testC{
	void work(int l,int r){
		tree an = query(1,1,n,l,r);
		int lca = LCA(an.maxid,an.minid);
		int ans = de[lca];
		printf("%d\n",ans);
	}
}
namespace testB{
	int ma[N],mi[N];
	void work(){
		q=read();
		int l,r,k;
		while(q--){
			int lma=1,rma=1,lmi=1,rmi=1;
			memset(ma,0,sizeof ma);
			memset(mi,0,sizeof mi);
			l=read(),r=read(),k=read();
			int ans=0;
			if(k==r-l+1) {
				testC::work(l,r);
				continue;
			} 
			if(k==1){
				for(int i=l;i<=r;i++){
					ans = max(ans,de[i]);
				}
				printf("%d\n",ans);
				continue;
			}
			for(int i=l;i<l+k-1;i++){
				while(dfn[i]>dfn[ma[rma-1]] && lma<rma) rma--;
				while(dfn[i]<dfn[mi[rmi-1]] && lmi<rmi) rmi--;
				ma[rma++] = i;
				mi[rmi++] = i;
			}
			//cout<<"ma="<<ma[lma]<<endl;
			for(int i=l+k-1;i<=r;i++){
				while(dfn[i]>dfn[ma[rma-1]] && lma<rma) rma--;
				while(dfn[i]<dfn[mi[rmi-1]] && lmi<rmi) rmi--;
				ma[rma++] = i;
				mi[rmi++] = i;
				if(ma[lma]<i-k+1) lma++;
				if(mi[lmi]<i-k+1) lmi++;
				//cout<<"ma="<<ma[lma]<<" mi="<<mi[lmi]<<endl;
				int lca = LCA(ma[lma],mi[lmi]);
				ans = max(ans,de[lca]);
			}
			printf("%d\n",ans);
		}
	}
}
namespace testD{
	void work(){
		q=read();
		int l,r,k;
		while(q--){
			l=read(),r=read(),k=read();
			tree an = query(1,1,n,l,r);
			int lca = LCA(an.maxid,an.minid);
			int ans = de[lca];
			printf("%d\n",ans);
		}
	}
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read(); ischain=1;
	for(int i=1,x,y;i<=n-1;i++){
		x=read(),y=read();
		cnt[x]++;
		link(x,y); link(y,x);
		if(cnt[x]>=2) ischain=0;
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,1,n);
	if(ischain) testD::work();
	else testB::work();
	return 0;
}
