#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;bool flg2=1;
int read(){int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f; 
}
void write(int x){
	if(x<10)return putchar(x^48),void();
	write(x/10),putchar((x%10)^48); 
}
bool Mde;
int n,q,dfn[N],lg[N],mn[N][20],d[N];
int head[N],cnt,tot;struct node{int to,nxt;}e[N<<1];
void add(int u,int v){e[++cnt]={v,head[u]},head[u]=cnt;} 
int my_min(int x,int y){return dfn[x]<dfn[y]?x:y; }
void init(int u,int fa){dfn[u]=++tot,mn[tot][0]=fa;int ct=0;
	for(int i=head[u],v;i;i=e[i].nxt)if((v=e[i].to)!=fa)ct++,d[v]=d[u]+1,init(v,u);
	flg2&=ct<=1;
}
int lca(int u,int v){
	if(u==v)return u;
	if((u=dfn[u])>(v=dfn[v]))swap(u,v);
	int len=lg[v-u++];
	return my_min(mn[u][len],mn[v-(1<<len)+1][len]);
}
struct SUB1{
	int ans[5005][5005];
	void work(){
		for(int i=1;i<=n;i++)for(int j=i,lc=i;j<=n;j++)lc=lca(lc,j),ans[j-i+1][i]=d[lc];
		scanf("%d",&q);
		for(int i=1,x,y,z;i<=q;i++){
			scanf("%d%d%d",&x,&y,&z);
			int mx=0;
			for(int j=x;j+z-1<=y;j++)mx=max(mx,ans[z][j]);
			printf("%d\n",mx);
		}
	}	
}T1;
struct SUB2{
	int ans[N],id[N],iid[N],idf[N];
	struct QUE{int l,r,k;}a[N];
	struct node{int mx,lm,rm;bool p;
		void merge(node &x,node &y){p=x.p|y.p;
			mx=max(max(x.mx,y.mx),x.rm+y.lm);
			if(x.p)lm=x.lm+y.lm;
			else lm=x.lm;
			if(y.p)rm=x.rm+y.rm;
			else rm=y.rm;
		}
	}tr[N<<2],res,tmp;
	void update(int o,int l,int r,int x,int y){
		if(l==r)return tr[o]={y,y,y,y},void();
		int mid=(l+r)>>1;
		if(x<=mid)update(o<<1,l,mid,x,y);
		else update(o<<1|1,mid+1,r,x,y);
		tr[o].merge(tr[o<<1],tr[o<<1|1]);
	}
	void query(int o,int l,int r,int x,int y){
		if(x<=l&&r<=y)return tmp.merge(res,tr[o]),res=tmp,void();
		int mid=(l+r)>>1;
		if(x<=mid)query(o<<1,l,mid,x,y);
		if(y>mid)query(o<<1|1,mid+1,r,x,y);
	}
	void solve(int l,int r,int L,int R){
		if(L>R)return ;
		if(l==r){
			for(int i=L;i<=R;i++)ans[id[i]]=n-l+1;
			return ;
		}
		int mid=(l+r)>>1;
		for(int i=l;i<=mid;i++)update(1,1,n,idf[n-i+1],1);
		int tl=L,tr=R;
		
		for(int i=L;i<=R;i++){
			res={0,0,0,0};
			query(1,1,n,a[id[i]].l,a[id[i]].r);
			if(res.mx>=a[id[i]].k)iid[tl]=id[i],tl++;
			else iid[tr]=id[i],tr--; 
		}
		for(int i=L;i<=R;i++)id[i]=iid[i];
		solve(mid+1,r,tr+1,R);
		for(int i=l;i<=mid;i++)update(1,1,n,idf[n-i+1],0);
		solve(l,mid,L,tl-1);
	}
	void work(){
		q=read();
		for(int i=1,x,y,z;i<=q;i++)x=read(),y=read(),z=read(),a[i]={x,y,z};
		for(int i=1;i<=n;i++)id[i]=i,idf[dfn[i]]=i;
		solve(1,n,1,q);//exit(0);
		for(int i=1;i<=q;i++)write(ans[i]),puts("");//("%d\n",ans[i]);
	} 
}T2; 
bool Med;
int main(){lg[0]=-1;
	for(int i=1;i<N;i++)lg[i]=lg[i>>1]+1;
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	for(int i=1,x,y;i<n;i++)x=read(),y=read(),add(x,y),add(y,x);
	d[1]=1,init(1,0);
	for(int j=1;j<=lg[n];j++)for(int i=1;i+(1<<j)-1<=n;i++)mn[i][j]=my_min(mn[i][j-1],mn[i+(1<<j-1)][j-1]);
	if(n<=5000)return T1.work(),0;
	else if(flg2)return T2.work(),0;
	return 0;
}

