#include<bits/stdc++.h>
#define ll int
#define N 5005
#define M 500005
using namespace std;
struct node{
    int t,n;
}edge[M<<1];
struct no{
    int l,r,w;
}shu[M<<2];
struct nd{
    int w,id;
    friend bool operator<(nd a,nd b){
        return a.w<b.w;
    }
}st[M*20][18],ji[M*20];
int head[M],cn=-1,n,q,nt,lg[M*20],lu[M],dep[M],ans,lc[N][N],la[N][N];
ll read(){char c=getchar();
    ll n=0;
    while(c<'0'||c>'9')c=getchar();
    while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=getchar();
    return n;
}
void add(ll a,ll b){
    edge[++cn]={b,head[a]};
    head[a]=cn;
    edge[++cn]={a,head[b]};
    head[b]=cn;
}
void dfs0(int f,int la){ll i;
    dep[f]=dep[la]+1;
    if(nt+1>=1e6)return;
    ji[++nt]={dep[f],f},lu[f]=nt;
    for(i=head[f];~i;i=edge[i].n){
        if(edge[i].t==la)continue;
        dfs0(edge[i].t,f);
        if(nt+1>=1e6)return;
        ji[++nt]={dep[f],f};
    }
}
void dfs1(ll f,ll la,ll fa,ll k){ll i;
    lc[k][f]=fa;
    for(i=head[f];~i;i=edge[i].n){
        if(edge[i].t==la)continue;
        dfs1(edge[i].t,f,dep[edge[i].t]<dep[fa]?edge[i].t:fa,k);
    }
}
ll lca(ll x,ll y){ll len;
    if(lu[x]>lu[y])swap(x,y);
    x=lu[x],y=lu[y],len=y-x+1;
    return min(st[x][lg[len]],st[y-(1<<lg[len])+1][lg[len]]).id;
}
void build(ll l,ll r,ll k){
    shu[k].l=l,shu[k].r=r;
    if(l==r){
        shu[k].w=l;
        return;
    }
    ll m=l+r>>1;
    build(l,m,k<<1),build(m+1,r,k<<1|1);
    shu[k].w=lca(shu[k<<1].w,shu[k<<1|1].w);
}
ll query(ll l,ll r,ll k){ll m,ans;
    if(l<=shu[k].l&&shu[k].r<=r)return shu[k].w;
    m=shu[k].l+shu[k].r>>1;
    if(l<=m)ans=query(l,r,k<<1);
    if(l>m&&r>m)ans=query(l,r,k<<1|1);
    else if(r>m)ans=lca(query(l,r,k<<1|1),ans);
    return ans;
}
int main(){ll i,k,x,y,l,r,le;bool p=true;
    freopen("query.in","r",stdin);
    freopen("query.out","w",stdout);
    memset(head,-1,sizeof(head));
    ios::sync_with_stdio(0);
    cout.tie(0);
    n=read();
    for(i=1;i<n;i++){x=read(),y=read();
        add(x,y);
    }
    dfs0(1,0);
    if(n<=5000){
        for(i=1;i<=n;i++)dfs1(i,0,i,i);
        for(i=1;i<=n;i++){la[i][i]=i;
            for(k=i+1;k<=n;k++)la[i][k]=lc[la[i][k-1]][k];
        }
        q=read();
        for(i=1;i<=q;i++){l=read(),r=read(),le=read(),ans=0;
            for(k=l;k+le-1<=r;k++)ans=max(dep[la[k][k+le-1]],ans);
            cout<<ans<<'\n';
        }
        return 0;
    }
    for(i=1;i<=nt;i++){
        if(i!=1)lg[i]=lg[i>>1]+1;
        st[i][0]=ji[i];
    }
    for(i=1;(1<<i)<=nt;i++){
        for(k=1;k+(i<<1)-1<=nt;k++){
            st[k][i]=min(st[k][i-1],st[k+(1<<i-1)][i-1]);
        }
    }
    build(1,n,1);
    q=read();
    for(i=1;i<=q;i++){l=read(),r=read(),le=read(),ans=0;
        for(k=l;k+le-1<=r;k++)ans=max(ans,dep[query(k,k+le-1,1)]);
        cout<<ans<<'\n';
    }
}