#include<cstdio>
namespace fastI{
    const int siz=1<<20;
    char buf[siz],*p=buf,*pend=buf;
    #define gc() ((p==pend&&(pend=(p=buf)+fread(buf,1,siz,stdin)),p==pend?EOF:*p++))
    int inp(){
        int x=0,f=1;
        char c=gc();
        while(c<'0'||c>'9'){
            if(c=='-')f=-1;
            c=gc();
        }
        while(c>='0'&&c<='9'){
            x=x*10+c-'0';
            c=gc();
        }
        return x*f;
    }
    #undef gc
}
using fastI::inp;
#define N_ 5005
#define N 5000
int max_s(int &p,int q){
    if(q>p)p=q;
}
int n,T;
struct eg{
    int to,last;
}edge[N_<<1];
int head[N_];
int fa[N_],dep[N_];
int dfn[N_],dfe[N_];
void dfs(int u,int f){
    static int cnt=0;
    dfn[u]=dfe[u]=++cnt;
    fa[u]=f,dep[u]=dep[f]+1;
    for(int i=head[u],v;i;i=edge[i].last){
        v=edge[i].to;
        if(v==f)continue;
        dfs(v,u);
        dfe[u]=dfe[v];
    }
}
int ans[N_][N_];
int main(){
    freopen("query.in","r",stdin);
    freopen("query.out","w",stdout);
    n=inp();
    if(n>N){
        for(int i=1;i<n;++i)inp(),inp();
        T=inp();
        while(T--)printf("1\n");
        return 0;
    }
    for(int i=1,u,v;i<n;++i){
        u=inp(),v=inp();
        edge[i*2-1]={v,head[u]},head[u]=i*2-1;
        edge[i*2]={u,head[v]},head[v]=i*2;
    }
    dfs(1,0);
    for(int i=1;i<=n;++i){
        ans[1][i]=dep[i];
        for(int u=i,j=i;(++j)<=n;){
            while(!(dfn[u]<=dfn[j]&&dfe[u]>=dfn[j]))u=fa[u];
            ans[j-i+1][i]=dep[u];
        }
    }
    T=inp();
    for(int l,r,k,res;T;--T){
        l=inp(),r=inp(),k=inp();
        res=0;
        for(int i=r-k+1;i>=l;--i)max_s(res,ans[k][i]);
        printf("%d\n",res);
    }
    return 0;
}
