#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
vector <int> g[N];
int Min[N][20], seq[N];
int pos[N], LG2[N], dep[N], res[N], stk[N], top, indx;
void dfs(int x,int fa)
{
indx++, seq[indx]=dep[x], pos[x]=indx;
for(auto to:g[x])
if(to!=fa)
{
dep[to]=dep[x]+1;
dfs(to,x);
seq[++indx]=dep[x];
}
}
struct que1
{
int pos,l,r,k,id;
};
struct que2
{
int L,R,k,id;
};
vector <que1> vec1[N], p1[N];
vector <que2> vec2[N], p2[N];
vector <int> num[N];
int val[N];
inline int query(int l,int r,int k)
{
int L=l, R=r-k+1, pos=l-1;
while(L<=R)
{
int mid=(L+R)>>1;
if(val[mid]<=val[mid+k-1])
pos=mid, L=mid+1;
else
R=mid-1;
}
int ans=0;
if(pos>=l)
ans=max(ans,val[pos]);
if(pos+k<=r)
ans=max(ans,val[pos+k]);
return ans;
}
inline void calc(int lmin,int lmax,int l,int r,int k,int id,int p1,int p2)
{
lmin=max(lmin,l);
lmax=min(lmax,r-k+1);
if(lmin<=lmax)
vec1[p1].push_back((que1){p2,lmin,lmax+k-1,k,id});
}
int Max[N*4];
void build(int rt,int l,int r)
{
if(l==r)
{
Max[rt]=val[l];
return;
}
int mid=(l+r)>>1;
build(rt*2,l,mid);
build(rt*2+1,mid+1,r);
Max[rt]=max(Max[rt*2],Max[rt*2+1]);
}
int query(int rt,int l,int r,int ql,int qr)
{
if(ql<=l && r<=qr)
return Max[rt];
int mid=(l+r)>>1, ans=0;
if(ql<=mid)
ans=max(ans,query(rt*2,l,mid,ql,qr));
if(mid+1<=qr)
ans=max(ans,query(rt*2+1,mid+1,r,ql,qr));
return ans;
}
int main()
{
freopen("query.in","r",stdin);
freopen("query.out","w",stdout);
int n;
cin >> n;
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d %d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dep[1]=1, dfs(1,-1);
for(int i=1;i<=indx;i++)
Min[i][0]=seq[i], LG2[i]=log2(i);
for(int i=1;i<20;i++)
for(int j=1;j+(1<<i)-1<=indx;j++)
Min[j][i]=min(Min[j][i-1],Min[j+(1<<(i-1))][i-1]);
int q;
cin >> q;
for(int i=1;i<=q;i++)
{
int l,r,k;
scanf("%d %d %d",&l,&r,&k);
int p=LG2[k], nexl=(l+(1<<p)-1)/(1<<p)*(1<<p), nexr=r/(1<<p)*(1<<p);
calc(nexl-(1<<p)+1,nexl,l,r,k,i,p,nexl/(1<<p));
if(nexl<nexr-(1<<p))
calc(nexr-(1<<p)-(1<<p)+1,nexr-(1<<p),l,r,k,i,p,(nexr-(1<<p))/(1<<p));
if(nexl<nexr)
calc(nexr-(1<<p)+1,nexr,l,r,k,i,p,nexr/(1<<p));
int L=nexl/(1<<p)+1, R=nexr/(1<<p)-2;
if(L<=R)
vec2[p].push_back((que2){L,R,k,i});
}
for(int k=0;k<20;k++)
{
for(int j=0;j<=n/(1<<k)+1;j++)
p1[j].clear(), p1[j].shrink_to_fit();
for(auto x:vec1[k])
p1[x.pos].push_back(x);
int l=(1<<k), r=min(n,(1<<(k+1))-1);
for(int j=l;j<=r+1;j++)
p2[j].clear(), num[j].clear(), num[j].shrink_to_fit(), p2[j].shrink_to_fit();
for(auto x:vec2[k])
p2[x.k].push_back(x);
for(int j=1;j<=n/(1<<k);j++)
{
int L=(j-1)*(1<<k)+1, R=min(n,(j+2)*(1<<k)), mid=j*(1<<k), v=pos[mid];
for(int k=L;k<=R;k++)
{
int l,r;
if(pos[k]<v)
l=pos[k], r=v;
else
l=v, r=pos[k];
int K=LG2[r-l+1];
val[k]=min(Min[l][K],Min[r-(1<<K)+1][K]);
}
for(int k=mid-1;k>=L;k--)
val[k]=min(val[k],val[k+1]);
for(int k=mid+1;k<=R;k++)
val[k]=min(val[k],val[k-1]);
for(auto x:p1[j])
res[x.id]=max(res[x.id],query(x.l,x.r,x.k));
for(int i=l;i<=r;i++)
if(mid+i-1<=n)
num[i].push_back(query(L,mid+i-1,i));
}
for(int i=l;i<=r;i++)
{
for(int j=0;j<num[i].size();j++)
val[j+1]=num[i][j];
if(num[i].size())
{
build(1,1,(int)num[i].size());
for(auto x:p2[i])
res[x.id]=max(res[x.id],query(1,1,num[i].size(),x.L,x.R));
}
}
}
for(int i=1;i<=q;i++)
printf("%d\n",res[i]);
return 0;
}