#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
inline char gc() {
	static char buf[1<<20],*l,*r;
	return l==r&&(r=(l=buf)+fread(buf,1,1<<20,stdin),l==r)?-1:*l++;
}
template<class T> void read(T &&x) {
	x=0;
	char c=gc();
	bool neg=false;
	for(; !isdigit(c); c=gc()) if(c=='-') neg=true;
	for(; isdigit(c); c=gc()) x=(x<<1)+(x<<3)+(c&15);
	if(neg) x=-x;
}
using namespace std;
const int N = 5e5, logN = 20;
int n,q;
vector<int> e[N + 2];
int anc[logN][N + 2],dep[N + 2];
vector<int> d[N + 2];
void dfs(int u,int p) {
	anc[0][u]=p;
	for(int i=1; i<logN; ++i)
		anc[i][u]=anc[i-1][anc[i-1][u]];
	for(auto v : e[u])
		if(v!=p) {
			dep[v]=dep[u]+1;
			dfs(v,u);
		}
}
int lca(int u,int v) {
	if(dep[u]<dep[v]) swap(u,v);
	for(int i=logN-1; i>=0; --i)
		if(dep[anc[i][u]]>=dep[v])
			u=anc[i][u];
	if(u==v) return u;
	for(int i=logN-1; i>=0; --i)
		if(anc[i][u]!=anc[i][v])
			u=anc[i][u],v=anc[i][v];
	return anc[0][u];
}
int st[logN][N + 2];
int rt[N + 2];
namespace pseg {
	const int S = N << 5;
	int tot,ls[S + 2],rs[S + 2];
	struct node {
		int l,r,s;
		bool t;
	} s[S + 2];
	node operator+(const node &x,const node& y) {
		return (node){
			x.l+(x.t?y.l:0),
			y.r+(y.t?x.r:0),
			max(max(x.s,y.s),x.r+y.l),
			x.t&&y.t,
		};
	}
#define mid (l+r>>1)
	int build(int l,int r) {
		int u=++tot;
		if(l==r) {
			s[u]={0,0,0,false};
			return u;
		}
		ls[u]=build(l,mid);
		rs[u]=build(mid+1,r);
		s[u]=s[ls[u]]+s[rs[u]];
		return u;
	}
	int modi(int v,int l,int r,int x) {
		int u=++tot;
		ls[u]=ls[v],rs[u]=rs[v],s[u]=s[v];
		if(l==r) {
			s[u]={1,1,1,true};
			return u;
		}
		if(x<=mid) ls[u]=modi(ls[u],l,mid,x);
		else rs[u]=modi(rs[u],mid+1,r,x);
		s[u]=s[ls[u]]+s[rs[u]];
		return u;
	}
	node query(int u,int l,int r,int x,int y) {
		if(r<x||y<l) return (node){0,0,0,false};
		if(x<=l&&r<=y) return s[u];
		if(y<=mid) return query(ls[u],l,mid,x,y);
		if(mid<x) return query(rs[u],mid+1,r,x,y);
		return query(ls[u],l,mid,x,y)+query(rs[u],mid+1,r,x,y);
	}
#undef mid
}
int main() {
	freopen("query.in","r",stdin);
	freopen("query.out","w",stdout);
	read(n);
	for(int i=1; i<n; ++i) {
		int x,y;
		read(x),read(y);
		e[x].emplace_back(y);
		e[y].emplace_back(x);
	}
	dep[1]=1;
	dfs(1,1);
	for(int i=1; i<n; ++i) d[dep[lca(i,i+1)]].push_back(i);
	rt[n+1]=pseg::build(1,n);
	for(int i=n; i>=1; --i) {
		rt[i]=rt[i+1];
		for(auto j : d[i])
			rt[i]=pseg::modi(rt[i],1,n,j);
	}
	for(int i=1; i<=n; ++i) st[0][i]=dep[i];
	for(int j=1; j<logN; ++j)
		for(int i=1; i+(1<<j)-1<=n; ++i)
			st[j][i]=max(st[j-1][i],st[j-1][i+(1<<j-1)]);
	read(q);
	for(int i=1; i<=q; ++i) {
		int l,r,k;
		read(l),read(r),read(k);
		if(k==1) {
			k=__lg(r-l+1);
			printf("%d\n",max(st[k][l],st[k][r-(1<<k)+1]));
			continue;
		}
		int s=1,t=n;
		while(s<=t) {
			int mid=(s+t)/2;
			if(pseg::query(rt[mid],1,n,l,r-1).s+1>=k) s=mid+1;
			else t=mid-1;
		}
		printf("%d\n",s-1);
	}
	return 0;
}