#include<bits/stdc++.h>
#define ri register int
#define ll long long
#define rll register ll
#define ull unsigned ll
#define fo(i,x,y) for(ri i=(x);i<=(y);++i)
#define fu(i,x,y) for(ri i=(x);i<(y);++i)
#define fd(i,x,y) for(ri i=(x);i>=(y);--i)
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define md (l+r>>1)
#define Ls l,md,g<<1
#define Rs md+1,r,g<<1|1
using namespace std;
const int N=1e4+5,K=500005<<2;
int n,Q,ed[N],la[N],tp,s[N][N];
int v[N],st[N],ans[N],dep[N],d[K],d2[K],d3[K];
vector<int>G[N];
struct nd{
	int l,r,k,h;
}q[N],t[N];
inline void dfs(ri x,ri fa){
	la[x]=++tp;
	st[tp]=x;dep[x]=dep[fa]+1;
	v[tp]=dep[x];
	for(auto y:G[x]){
		if(y==fa)continue;
		dfs(y,x);st[++tp]=x;
		v[tp]=dep[x];
	}
	ed[x]=tp;
}
inline void work(){
	fo(i,1,n-1){
		ri x,y;scanf("%d%d",&x,&y);
		G[x].pb(y);G[y].pb(x);
	}
	scanf("%d",&Q);
	fo(i,1,Q){
		scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);
		q[i].h=i;
	}
	sort(q+1,q+1+Q,[&](nd x,nd y){return x.k<y.k;});
	dfs(1,0);
	fo(i,1,tp){
		ri mn=1e9;
		fo(j,i,tp){
			mn=min(mn,v[j]);
			s[i][j]=mn;
		}
	}
	fo(i,1,n){
		t[i].l=la[i];t[i].r=ed[i];
	}
	ri nw=1;
	fo(i,1,Q){
		while(nw<q[i].k){
			fo(j,1,n){
				if(j+nw>n)break;
				t[j].l=min(t[j].l,la[j+nw]);
				t[j].r=max(t[j].r,ed[j+nw]);
			}
			++nw;
		}
		ri sm=0;
		fo(j,q[i].l,q[i].r){
			if(j+q[i].k-1>q[i].r)break;
			sm=max(sm,s[t[j].l][t[j].r]);
		}
		ans[q[i].h]=sm;
	}
	fo(i,1,Q)printf("%d\n",ans[i]);
}
inline void build(ri l,ri r,ri g){
	if(l==r){
		d[g]=v[l];
		d2[g]=st[l];
		d3[g]=ed[l];
		return;
	}
	build(l,md,g<<1);
	build(md+1,r,g<<1|1);
	d[g]=min(d[g<<1],d[g<<1|1]);
	d2[g]=min(d2[g<<1],d2[g<<1|1]);
	d3[g]=max(d3[g<<1],d3[g<<1|1]);
}
inline int que1(ri l,ri r,ri g,ri L,ri R){
	if(l>R||r<L)return 1e9;
	if(l>=L&&r<=R)return d[g];
	return min(que1(Ls,L,R),que1(Rs,L,R));
}
inline int que2(ri l,ri r,ri g,ri L,ri R){
	if(l>R||r<L)return 1e9;
	if(l>=L&&r<=R)return d2[g];
	return min(que2(Ls,L,R),que2(Rs,L,R));
}
inline int que3(ri l,ri r,ri g,ri L,ri R){
	if(l>R||r<L)return 0;
	if(l>=L&&r<=R)return d3[g];
	return max(que3(Ls,L,R),que3(Rs,L,R));
}
inline void work2(){
	fo(i,1,n-1){
		ri x,y;scanf("%d%d",&x,&y);
		G[x].pb(y);G[y].pb(x);
	}
	scanf("%d",&Q);
	fo(i,1,Q){
		scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);
		q[i].h=i;
	}
	dfs(1,0);
	build(1,tp,1);
	fo(i,1,Q){
		ri l=que2(1,n,1,q[i].l,q[i].r);
		ri r=que3(1,n,1,q[i].l,q[i].r);
		ri s=que1(1,n,1,l,r);
		printf("%d\n",s);
	}
}
int main(){
	freopen("query.in","r",stdin);
//	freopen("query2.in","r",stdin);
	freopen("query.out","w",stdout);
	scanf("%d",&n);
	if(n<=5e3){
		work();
	}
	else{
		work2();
	}
}
