#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL MAXN=5e5+5;
template<typename T>inline void read(T &x){
    bool f=1;x=0;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=!f;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?x:-x;return ;
}
template<typename T>inline void write(T x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');return ;
}
LL n,q,ql[MAXN],qr[MAXN],qk[MAXN];
vector<LL>a[MAXN];
LL f[MAXN][22],dep[MAXN];
void dfs(LL u,LL fa){
    dep[u]=dep[fa]+1;
    for(LL i=1;i<=20;i++){
        f[u][i]=f[f[u][i-1]][i-1];
    }
    for(LL i=0;i<a[u].size();i++){
        LL v=a[u][i];
        if(v==fa)continue;
        f[v][0]=u;
        dfs(v,u);
    }
}
LL ans[5005][5005];
LL lca(LL x,LL y){
    if(dep[x]<dep[y])swap(x,y);
    for(LL i=19;i>=0;i--){
        if(dep[f[x][i]]>=dep[y]){
            x=f[x][i];
        }
    }
    if(x==y)return x;
    for(LL i=19;i>=0;i--){
        if(f[x][i]!=f[y][i]){
            x=f[x][i];y=f[y][i];
        }
    }
    return f[x][0];
}
void solve1(){
    for(LL i=1;i<=n;i++){
        LL now=i;
        ans[i][i]=dep[now];
        for(LL j=i+1;j<=n;j++){
            now=lca(now,j);
            ans[i][j]=dep[now];
            // cout<<i<<" "<<j<<" "<<now<<" "<<ans[i][j]<<endl;
        }
    }
    for(LL i=1;i<=q;i++){
        LL maxn=0;
        for(LL j=ql[i];j<=qr[i];j++){
            if(j+qk[i]-1<=qr[i])maxn=max(maxn,ans[j][j+qk[i]-1]);
        }
        printf("%lld\n",maxn);
    }
}
LL lcaa[MAXN][22];
void solve2(){
    for(LL i=1;i<=n;i++){
        lcaa[i][0]=i;
    }
    for(LL j=1;j<=19;j++){
        for(LL i=1;i<=n;i++){
            lcaa[i][j]=lca(lcaa[i][j-1],lcaa[i+(1ll<<(j-1))][j-1]);
        }
    }
    for(LL i=1;i<=q;i++){
        LL now=ql[i],anss=ql[i];
        for(LL j=19;j>=0;j--){
            if(qk[i]&(1ll<<j)){
                anss=lca(anss,lcaa[now][j]);
                now+=(1ll<<j);
            }
        }
        write(dep[anss]);
        putchar('\n');
    }
}
int main(){
    freopen("query.in","r",stdin);
    freopen("query.out","w",stdout);
    bool flga=true,flgb=true;
    read(n);
    for(LL i=1;i<n;i++){
        LL u,v;
        read(u);read(v);
        a[u].push_back(v);a[v].push_back(u);
    }
    read(q);
    for(LL i=1;i<=q;i++){
        read(ql[i]);read(qr[i]);read(qk[i]);
        if(qk[i]!=qr[i]-ql[i]+1)flgb=false;
    }
    dfs(1,0);
    if(n<=5000){
        solve1();
        return 0;
    }
    if(flgb){
        solve2();
        return 0;
    }
    solve2();
}