#include<bits/stdc++.h> using namespace std; const int maxn=2e5+7,maxm=4e5+7; typedef long long ll; inline ll read(){ ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } int head[maxn],to[maxm],Next[maxm],tot; ll val[maxm]; struct node{ int id,l,r; ll dis,w; bool operator<(const node& a)const{ return dis>a.dis; } }q[maxn]; struct sum{ int l,r; ll dis; bool operator<(const sum& a)const{ return dis>a.dis; } }a[maxn]; void add(int x,int y,ll c) { to[++tot]=y; Next[tot]=head[x]; val[tot]=c; head[x]=tot; } int num; void dfs(int u,int fa) { a[u].l=++num; for(int i=head[u];i;i=Next[i]) { int v=to[i]; if(v==fa) continue; a[v].dis=a[u].dis+val[i]; dfs(v,u); } a[u].r=num; } ll cnt[maxn],all[maxn],n; #define lowbit(x) (x&(-x)) void add(int x,ll val) { while(x<=n) { cnt[x]+=1; all[x]+=val; x+=lowbit(x); } } ll query(int x,int op) { ll res1=0,res2=0; while(x) { res1+=cnt[x]; res2+=all[x]; x-=lowbit(x); } return op?res1:res2; } ll ans[maxn]; int main() { n=read(); for(int i=2,p,c;i<=n;++i) { p=read(),c=read(); add(p,i,c); add(i,p,c); } dfs(1,0); int Q=read(); for(int i=1,x,k;i<=Q;++i) { x=read(),k=read(); q[i].id=i; q[i].dis=a[x].dis+k; q[i].w=a[x].dis; q[i].l=a[x].l; q[i].r=a[x].r; } sort(q+1,q+1+Q); sort(a+1,a+1+n); for(int i=1,j=1;i<=Q;++i) { while(a[j].dis>=q[i].dis) { add(a[j].l,a[j].dis); ++j; } ans[q[i].id]=query(q[i].r,0)-query(q[i].l,0); ans[q[i].id]-=(query(q[i].r,1)-query(q[i].l,1))*q[i].w; } for(int i=1;i<=Q;++i) printf("%lld\n",ans[i]); }