#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	#define fr(i,x,y) for(ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define il inline
	#define db double
	#define pf printf
	#define pc putchar
	#define MP make_pair
	#define fir first
	#define sec second
	#define space pc(' ')
	#define enter pc('\n')
	il ll read(){
		ll sum=0,fu=0;
		char ch=getchar();
		while(!isdigit(ch)){
			if(ch=='-') fu=1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch-'0');
			ch=getchar();
		}
		return fu?(-sum):sum;
	}
	void write(ll x){
		if(x<0){
			pc('-');
			x=-x;
		}
		if(x>9) write(x/10);
		pc('0'+(x%10));
	}
	il void writesp(ll x){
		write(x);
		space;
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
}
using namespace my_std;
ll n,q,head[500050],cnt=0,d[500050];
ll dep[500050],f[500050],st[500050],ed[500050],tot=0;
int res[5050][5050];
struct edge{
	ll nxt,to;
}e[1000010];
struct node{
	ll l,r,k,ans;
}p[500050];
void add(ll u,ll v){
	e[++cnt].nxt=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
struct subA{
	ll id[500050],mp[500050];
	void solve(){
		id[1]=1;
		fr(j,2,n){
			go(id[j-1]){
				ll v=e[i].to;
				if(v==id[j-2]) continue;
				id[j]=v;
				break;
			}
		}
		fr(i,1,n) mp[id[i]]=i;
	}
}subA;
struct subB{
	vector<ll> vec[500050];
	ll g[500050][20],st[500050],val[500050],top=0;
	void dfs(ll fa,ll u){
		g[u][0]=fa;
		fr(i,1,19) g[u][i]=g[g[u][i-1]][i-1];
		go(u){
			ll v=e[i].to;
			if(v==fa) continue;
			dfs(u,v);
		}
	}
	il ll get_lca(ll x,ll y){
		if(dep[x]<dep[y]) swap(x,y);
		pfr(i,19,0) if(dep[g[x][i]]>=dep[y]) x=g[x][i];
		if(x==y) return x;
		pfr(i,19,0){
			if(g[x][i]!=g[y][i]){
				x=g[x][i];
				y=g[y][i];
			}
		}
		return g[x][0];
	}
	void solve(){
		dfs(0,1);
		fr(i,1,q) vec[p[i].r].push_back(i);
		fr(i,1,n){
			st[++top]=i;
			val[top]=i;
			while(top>1){
				val[top]=get_lca(val[top],i);
				val[top-1]=get_lca(val[top-1],i);
				if(val[top]==val[top-1]) top--;
			}
			fr(j,0,(ll)vec[i].size()-1){
				ll id=vec[i][j];
				ll L=1,R=top;
				while(L<R){
					ll mid=(L+R+1)>>1;
					if(st[mid]>p[id].l) R=mid-1;
					else L=mid;
				}
				p[id].ans=val[L];
			}
		}
		fr(i,1,q) writeln(p[i].ans);
	}	
}subB;
void dfs1(ll fa,ll u){
	dep[u]=dep[fa]+1;
	f[u]=fa;
	st[u]=++tot;
	go(u){
		ll v=e[i].to;
		if(v==fa) continue;
		dfs1(u,v);
	}
	ed[u]=tot;
}
int main(){
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	n=read();
	fr(i,2,n){
		ll u=read(),v=read();
		add(u,v);
		add(v,u);
		d[u]++;
		d[v]++;
	}
	q=read();
	fr(i,1,q){
		p[i].l=read();
		p[i].r=read();
		p[i].k=read();
	}
	bl ckA=1,ckB=1;
	fr(i,1,n) if(d[i]>2) ckA=0;
	fr(i,1,n) if(p[i].k!=(p[i].r-p[i].l+1)) ckB=0;
//	if(ckA){
//		subA.solve();
//		return 0;
//	}
	if(ckB){
		subB.solve();
		return 0;
	}
	dfs1(0,1);
	fr(l,1,n){
		ll now=l;
		res[l][l]=dep[l];
		fr(r,l+1,n){
			while(st[r]<st[now]||st[r]>ed[now]) now=f[now];
			res[l][r]=dep[now];
		}
	}
	fr(i,1,q){
		int maxx=0;
		fr(j,p[i].l,p[i].r-p[i].k+1) maxx=max(maxx,res[j][j+p[i].k-1]);
		writeln(maxx);
	}
}
