//~11:21 #include using namespace std; // #define vc(_4,_3,_2,_1,_0,...) _0 // #define vsc(a...) vc(a,4,3,2,1) // #define cat(a,b) a##b // #define cats(a,b) cat(a,b) // #define deb1(a) <<#a<<"="<<(a)<<"; " // #define deb2(a,b) deb1(a)deb1(b) // #define deb3(a,b...) deb1(a)deb2(b) // #define deb4(a,b...) deb1(a)deb3(b) // #define deb(a...) cerr<<"deb"<<__LINE__<<"|: " cats(deb,vsc(a))(a) <<"\n" const int maxn=5e5+10; struct{ int nxt,v; }edge[maxn<<1]; int head[maxn],ec; void addedge(int u,int v){edge[++ec]={head[u],v};head[u]=ec;} struct node{mutable int l,r;}; bool operator<(node a,node b){return a.l&a,set&b,int dep){ if(a.size()=b.size()); // deb(b.size(),a.size()); // assert(__<=1e9); for(node cur:b){ int l=cur.l,r=cur.r; auto q=a.insert(cur).first; auto p=q++; if(q!=a.end()&&q->l==r+1)p=a.erase(p),q->l=l,t[++tc]={dep,q->l,q->r}; // if(p!=a.begin()&&prev(p)->r==l-1)prev(p)->r=r,a.erase(p); if(p!=a.begin()&&(--p)->r==l-1)l=p->l,p=a.erase(p),p->l=l,t[++tc]={dep,p->l,p->r}; } } setse[maxn]; void dfs(int u,int fa,int dep){ // assert(u>1; int j=l,rttp=0,lttp=l; int lptp=ql,rptp=0; for(int i=ql;i<=qr;++i){ while(j<=r && t[j].r-t[j].l+1>=q[i].k){ if(t[j].dep>=md){ upd(t[j].l,t[j].r); t[lttp++]=t[j++]; }else{ t_t[rttp++]=t[j++]; } } if(qmax(q[i].r-q[i].k+1) >= q[i].l+q[i].k-1){ q[lptp++]=q[i]; }else{ t_p[rptp++]=q[i]; } } for(int p=j;p<=r;++p) if(t[p].dep>=md)t[lttp++]=t[p]; else t_t[rttp++]=t[p]; // assert(lttp+rttp == r+1); // assert(lptp+rptp == qr+1); memcpy(t+lttp,t_t,rttp*sizeof t[0]); memcpy(q+lptp,t_p,rptp*sizeof q[0]); for(int p=l;p=md)reset(t[p].l); sol(l,lttp-1,ql,lptp-1,md,vr); sol(lttp,r,lptp,qr,vl,md-1); } signed main(){ freopen("query.in","r",stdin); freopen("query.out","w",stdout); ios::sync_with_stdio(0),cin.tie(0); cin>>n;for(int i=1,u,v;i>u>>v,addedge(u,v),addedge(v,u); // deb(__LINE__); dfs(1,0,1); cin>>Q; for(int i=1;i<=Q;++i){ int l,r,k;cin>>l>>r>>k; q[i]={l,r,k,i}; // int dep=0; // for(int i=1;i<=tc;++i){ // if(t[i].l<=r-k+1 && t[i].r>=l+k-1 && t[i].r-t[i].l+1 >= k) // dep=max(dep,t[i].dep); // } // cout<b.r-b.l;}); // for(int i=1;i<=tc;++i){ // deb(t[i]); // } sort(q+1,q+Q+1,[](auto a,auto b){return a.k>b.k;}); sol(1,tc,1,Q,1,n); for(int i=1;i<=Q;++i)cout<