#include<bits/stdc++.h>
using namespace std;
vector<int> e[500010];
int n,q,anc[500010][20],dep[500010],a[500010],ck=2;
struct query{
int l,r,k,ans,id;
bool operator <(const query b)const{
return k<b.k;
}
}qu[500010];
bool cmp(query a,query b){
return a.id<b.id;
}
struct seg{
int l,len,val;
list<int> app;
bool operator <(const seg b)const{
return l<b.l;
}
seg(){
app.clear();
}
};
struct mst{
int val[2000010];
mst(){
memset(val,0,sizeof(val));
}
void pushup(int rt){
val[rt]=max(val[rt<<1],val[rt<<1|1]);
}
void insert(int rt,int l,int r,int pos,int v){
if(r<pos||l>pos)
return;
if(l==r){
val[rt]=v;
return;
}
insert(rt<<1,l,l+r>>1,pos,v);
insert(rt<<1|1,(l+r>>1)+1,r,pos,v);
pushup(rt);
}
int query(int rt,int l,int r,int ql,int qr){
if(r<ql||l>qr)
return 0;
if(ql<=l&&r<=qr)
return val[rt];
return max(query(rt<<1,l,l+r>>1,ql,qr),query(rt<<1|1,(l+r>>1)+1,r,ql,qr));
}
}t1,t2;
set<seg> s;
set<int> bad_l;
void dfs(int cur,int fa){
anc[cur][0]=fa;
dep[cur]=dep[fa]+1;
t1.insert(1,1,n,cur,dep[cur]);
for(int i=1;i<20;i++)
anc[cur][i]=anc[anc[cur][i-1]][i-1];
for(auto g:e[cur])
if(g!=fa)
dfs(g,cur);
}
int lca(int x,int y){
if(dep[x]>dep[y])
swap(x,y);
for(int i=19;i>=0;i--)
if(dep[y]-dep[x]>=(1<<i))
y=anc[y][i];
if(x==y)
return x;
for(int i=19;i>=0;i--)
if(anc[y][i]!=anc[x][i]){
y=anc[y][i];
x=anc[x][i];
}
return anc[x][0];
}
void modify(){
assert((*(s.begin())).l==ck-1);
if(*(bad_l.lower_bound(ck))!=ck-1){
seg tmp=*s.begin();
s.erase(tmp);
if(tmp.len!=1){
tmp.len--;
tmp.l++;
s.insert(tmp);
}
}
vector<int> dtmp;
for(auto g:bad_l){
seg ftmp,tmp;
ftmp.l=g;
auto itmp=s.lower_bound(ftmp);
tmp=*itmp;
s.erase(itmp);
if(g!=ck-1){
tmp.app.push_front(tmp.app.front());
}
else{
tmp.l++;
tmp.len--;
}
t2.insert(1,1,n,tmp.l+tmp.len-1,0);
tmp.app.pop_back();
if(tmp.app.front()==tmp.app.back()){
tmp.val=tmp.app.front();
for(int i=tmp.l;i<tmp.l+tmp.len;i++)
t2.insert(1,1,n,i,0);
tmp.app.clear();
dtmp.push_back(g);
}
s.insert(tmp);
}
for(auto g:dtmp)
bad_l.erase(g);
ck++;
}
signed main(){
freopen("query.in","r",stdin);
freopen("query.out","w",stdout);
cin>>n;
for(int i=1,u,v;i<n;i++){
scanf("%d%d",&u,&v);
e[u].emplace_back(v);
e[v].emplace_back(u);
}
dfs(1,0);
for(int i=1;i<n;i++){
a[i]=dep[lca(i,i+1)];
}
cin>>q;
for(int i=0;i<q;qu[i].id=i,i++)
scanf("%d%d%d",&qu[i].l,&qu[i].r,&qu[i].k);
sort(qu,qu+q);
for(int i=1,left=1;i<=n;i++){
if(a[i]>a[i+1]){
seg tmp;
tmp.l=left;
tmp.len=i-left+1;
if(a[i]==a[left])
tmp.val=a[i];
else{
tmp.val=-1;
for(int j=left;j<=i;j++){
tmp.app.push_back(a[j]);
t2.insert(1,1,n,j,a[j]);
}
bad_l.insert(left);
}
s.insert(tmp);
left=i+1;
}
}
seg tmp;
tmp.l=n;
tmp.len=1;
tmp.val=0;
s.insert(tmp);
for(int i=0;i<q;i++){
if(qu[i].k==1){
qu[i].ans=t1.query(1,1,n,qu[i].l,qu[i].r);
continue;
}
while(qu[i].k>ck){
modify();
}
qu[i].l+=qu[i].k-2;
qu[i].r--;
seg ftmp;
ftmp.l=qu[i].l;
auto sit=prev(s.upper_bound(ftmp));
qu[i].ans=max((*sit).val,t2.query(1,1,n,qu[i].l,qu[i].r));
}
sort(qu,qu+q,cmp);
for(int i=0;i<q;i++)
printf("%d\n",qu[i].ans);
}