#include<bits/stdc++.h>
using namespace std;
#define ll int
#define lson (u<<1)
#define rson (u<<1|1)
const ll N=1000007;
int n,m,LG2[N<<1],fl=1,dfn[N],L,id[N<<1],dep[N],ans[N],P[20][N],mn[N<<2],pos[N<<2],dlca[N],FA[N][20],qwq[N<<2];
vector<int> to[N];
vector<pair<pair<int,int>,int> > q[N];
void dfs1(int u,int fa){
dep[u]=dep[fa]+1;
id[dfn[u]=++L]=u;fl&=(L<=n);
FA[u][0]=fa;
for (int i=1;i<20;++i) FA[u][i]=FA[FA[u][i-1]][i-1];
for (auto v:to[u]) if (v!=fa){
dfs1(v,u);
id[++L]=u;
}
}
int mnL[N<<2],mnR[N<<2],cl[N],cr[N];
vector<int> vec[N];
void modifyM(int u,int l,int r,int x,int v){
mn[u]=max(mn[u],v);
if (l==r) return;
int mid=l+r>>1;
if (x<=mid) modifyM(lson,l,mid,x,v);
else modifyM(rson,mid+1,r,x,v);
}
void modifyP(int u,int l,int r,int x,int v){
qwq[u]=min(qwq[u],v);
if (l==r) return;
int mid=l+r>>1;
if (x<=mid) modifyP(lson,l,mid,x,v);
else modifyP(rson,mid+1,r,x,v);
if (qwq[lson]==qwq[u]) pos[u]=pos[lson];else pos[u]=pos[rson];
}
void modifyL(int u,int l,int r,int x,int v){
mnL[u]=max(mnL[u],v);
if (l==r) return;
int mid=l+r>>1;
if (x<=mid) modifyL(lson,l,mid,x,v);
else modifyL(rson,mid+1,r,x,v);
}
void modifyR(int u,int l,int r,int x,int v){
mnR[u]=max(mnR[u],v);
if (l==r) return;
int mid=l+r>>1;
if (x<=mid) modifyR(lson,l,mid,x,v);
else modifyR(rson,mid+1,r,x,v);
}
int queryL(int u,int l,int r,int L,int R){
if (L<=l&&r<=R) return mnL[u];
int mid=l+r>>1;
if (R<=mid) return queryL(lson,l,mid,L,R);
if (L>mid) return queryL(rson,mid+1,r,L,R);
return max(queryL(lson,l,mid,L,R),queryL(rson,mid+1,r,L,R));
}
int queryM(int u,int l,int r,int L,int R){
if (L<=l&&r<=R) return mn[u];
int mid=l+r>>1;
if (R<=mid) return queryM(lson,l,mid,L,R);
if (L>mid) return queryM(rson,mid+1,r,L,R);
return max(queryM(lson,l,mid,L,R),queryM(rson,mid+1,r,L,R));
}
int queryR(int u,int l,int r,int L,int R){
if (L<=l&&r<=R) return mnR[u];
int mid=l+r>>1;
if (R<=mid) return queryR(lson,l,mid,L,R);
if (L>mid) return queryR(rson,mid+1,r,L,R);
return max(queryR(lson,l,mid,L,R),queryR(rson,mid+1,r,L,R));
}
pair<int,int> queryP(int u,int l,int r,int L,int R){
if (L<=l&&r<=R) return make_pair(qwq[u],pos[u]);
int mid=l+r>>1;
if (R<=mid) return queryP(lson,l,mid,L,R);
if (L>mid) return queryP(rson,mid+1,r,L,R);
auto pl=queryP(lson,l,mid,L,R),pr=queryP(rson,mid+1,r,L,R);
if (pl.first<=pr.first) return pl;return pr;
}
void dfsC(int l,int r){
if (l>r) return;
int u=queryP(1,1,n,l,r).second;
cl[u]=l;cr[u]=r;
vec[r-l+1].emplace_back(u);
dfsC(l,u-1);dfsC(u+1,r);
}
void build(int u,int l,int r){
pos[u]=l;
if (l==r) return;
int mid=l+r>>1;
build(lson,l,mid);build(rson,mid+1,r);
}
int LCA(int u,int v){
if (dep[u]<dep[v]) swap(u,v);
for (int i=19;~i;--i) if (dep[u]-dep[v]>>i) u=FA[u][i];
if (u==v) return u;
for (int i=19;~i;--i) if (FA[u][i]!=FA[v][i]){u=FA[u][i];v=FA[v][i];}
return FA[u][0];
}
void solve(){
cin>>m;
for (int i=1;i<=n;++i) modifyM(1,1,n,i,dep[i]);
for (int l,r,k,i=1;i<=m;++i){
cin>>l>>r>>k;
if (k==1) ans[i]=queryM(1,1,n,l,r);
else q[k-1].emplace_back(make_pair(make_pair(l,r-1),i));
}
build(1,1,n);
memset(qwq,0x3f,sizeof(qwq));
for (int i=1;i<n;++i) modifyP(1,1,n,i,(dlca[i]=dep[LCA(i,i+1)]));
dfsC(1,n-1);
for (int k=L;k;--k){
for (auto x:vec[k]){
modifyL(1,1,n,cl[x],dlca[x]);modifyR(1,1,n,cr[x],dlca[x]);
}
for (auto p:q[k]){
int l=p.first.first,r=p.first.second;
ans[p.second]=max(queryP(1,1,n,l,r).first,max(queryL(1,1,n,l,r-k+1),queryR(1,1,n,l+k-1,r)));
}
}
for (int i=1;i<=m;++i) cout<<ans[i]<<'\n';
}
namespace BF{
int LCA[5007][5007],timer,tI[N],tO[N],TMP[5007][5007];
void dfsB(int u,int fa){
tI[u]=tO[u]=++timer;id[timer]=u;
LCA[u][u]=u;dep[u]=dep[fa]+1;
for (auto v:to[u]) if (v!=fa){
dfsB(v,u);
for (int i=tI[u];i<=tO[u];++i) for (int j=tI[v];j<=tO[v];++j) LCA[id[i]][id[j]]=LCA[id[j]][id[i]]=u;
tO[u]=tO[v];
}
}
void solve(){
dfsB(1,0);
for (int i=1;i<=n;++i){
TMP[i][i]=i;
for (int j=i+1;j<=n;++j) TMP[i][j]=LCA[TMP[i][j-1]][j];
}
cin>>m;
for (int l,r,k,i=1;i<=m;++i){
cin>>l>>r>>k;
int ans=0;
for (int j=l;j<=r-k+1;++j) ans=max(ans,dep[TMP[j][j+k-1]]);
cout<<ans<<'\n';
}
}
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
freopen("query.in","r",stdin);
freopen("query.out","w",stdout);
cin>>n;
for (int i=1,u,v;i<n;++i){
cin>>u>>v;
to[u].emplace_back(v);to[v].emplace_back(u);
}
if (n<=5000){BF::solve();return 0;}
dfs1(1,0);
solve();
return 0;
}