C++#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <random>
#include <chrono>
#include <bitset>
#define FOR(i,a,b) for(int i = (a);i <= (b);++i)
#define REP(i,a,b) for(int i = (a);i >= (b);--i)
#define GO(x) for(int i = h[x],y = e[i];i;y = e[i=ne[i]])
#define bp __builtin_popcount
#define up std::unordered_map
#define ll long long
#define ull unsigned long long
static char stkk[200];
template<typename T>inline void output(T x){
if(!x)return putchar('0'),void();
if(x<0)x = ~x+1,putchar('-');
int top = 0;
for(;x;stkk[++top]=x%10^48,x/=10);
for(;top;putchar(stkk[top--]));
}
template<typename T>inline void readx(T &x){
x = 0;int y = 1;char c = getchar();
for(;c<48||c>58;c = getchar())if(c=='-')y = -1;
for(;c>=48&&c<=58;c = getchar())x = (x<<1)+(x<<3)+(c^48);
x *= y;
}
const int M = 54,N = 2e5+10+M,G = 20,mo = 998244353;
const int len = (1<<24);
const int b1 = 131,mo1 = 994842353;
const int b2 = 114514,mo2 = 1e9+7;
static int n,m,v[N],tf[N];
static int e[N],ne[N],h[N],tot;
static int pw[N];
inline void init_pw(int n){
pw[0] = 1;
FOR(i,1,n)pw[i] = 2ll*pw[i-1]%mo;
}
inline void add(int x,int y){
e[++tot] = y,ne[tot] = h[x],h[x] = tot;
}
static bool gnow;
static int dfn[N],fa[N],dep[N],st[N][G],lg[N],dfsct;
static int sz[N];
void dfs0(int x,int fr){
dep[x] = dep[st[dfn[x]=++dfsct][0]=fa[x]=fr]+(sz[x]=1);
GO(x)dfs0(y,x),sz[x]+=sz[y];
}
inline int get_min(int x,int y){
return dep[x]<dep[y]?x:y;
}
void init_st(int n){
lg[0] = -1;FOR(i,1,n)lg[i] = lg[i>>1]+1;
FOR(k,1,lg[n])FOR(i,1,n-pw[k]+1)
st[i][k] = get_min(st[i][k-1],st[i+pw[k-1]][k-1]);
}
inline int lca(int x,int y){
if(x==y)return x;
if((x=dfn[x])>(y=dfn[y]))std::swap(x,y);
int k = lg[y-x++];
return get_min(st[x][k],st[y-pw[k]+1][k]);
}
struct node{
int o[M],h[M],h2[M],tp;
void ins(int d){
o[++tp] = d;
h[tp] = (1ll*h[tp-1]*b1+(d+1))%mo1;
h2[tp] = (1ll*h2[tp-1]*b2+(d+1))%mo2;
}
void init(int x,int d){//leaf x,
if(x==0)return tp=-1,void();
int t,pre = bp(t=(x-1));
tp = pre;
for(;t;t>>=1,++d)if(t&1){
o[tp--] = d;
}
tp = pre;
FOR(i,1,tp)
h[i] = (1ll*h[i-1]*b1+(o[i]+1))%mo1,
h2[i] = (1ll*h2[i-1]*b2+(o[i]+1))%mo2;
}
int cmp(const node& A){//now < A,now = A,now > A
if(!tp&&!A.tp)return 0;
if(tp==0)return -1;
if(A.tp==0)return 1;
if(h[tp]==A.h[A.tp]&&h2[tp]==A.h2[tp])return 0;
int pre = std::min(tp,A.tp);
if(h[pre]==A.h[pre]&&h2[pre]==A.h2[pre])return tp==pre?-1:1;
int dl = 1,dr = pre-1,mid,ans = 0;
while(dl<=dr){
mid = (dl+dr)>>1;
if(h[mid]==A.h[mid]&&h2[mid]==A.h2[mid])ans = mid,dl = mid+1;
else dr = mid-1;
}
return o[ans+1]<A.o[ans+1]?-1:1;
}
void val(int &p1,int &p2){
p1 = h[tp],p2 = h2[tp];
}
int cal(){
int c = 1;
FOR(i,1,tp)c = (0ll+c+pw[o[i]])%mo;
return c;
}
void get(const node& A){
tp = A.tp;
FOR(i,1,tp)h[i] = A.h[i],o[i] = A.o[i],h2[i] = A.h2[i];
}
}a[N<<1];
struct node1{
int x;
bool operator<(const node1& A)const{
return a[x].cmp(a[A.x])==1;
}
};
static std::set<node1> ans;
struct node2{
int x,v;
bool operator<(const node2& A)const{
return v<A.v;
}
};
std::mt19937_64 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
const ull s1 = rnd(),s2 = rnd(),s3 = rnd();
static int q[N<<1],tl,idct;
inline int get_new(){
if(tl)return q[tl--];
while(++idct==2*n);
return idct;
}
struct my_hsh{
inline ull F(ull x){
x+=s1;
x=(x^(x>>33))*s2;
x=(x^(x>>30))*s3;
return x;
}
std::bitset<len> vis;
ull f[len];
int v[len],idct;
inline int pos(ull x){
int i = F(x)&(len-1);
while(vis[i]&&f[i]!=x)((++i)&=(len-1));
vis[i] = 1,f[i] = x;
return i;
}//pre use it,find used pos
int qy(ull x,bool flg){
int i = pos(x);
return flg?v[i]++:--v[i];
}
int qy2(ull x){
int i = pos(x);
return v[i]?v[i]:(v[i]=get_new());
}
void del(ull x){
int i = pos(x);
q[++tl] = v[i];
v[i] = 0;
//p1,p2 -> idct
}
}ctp,ctx,hsh;
static std::set<node2> s[N<<1];
inline bool ok(int x,int y){
return dfn[y]>dfn[x]+sz[x]-1;
}
inline void ins(int x,int id);
inline void del(int x,int id);
inline void renew_ad(int id,int p,bool flg){
a[0].get(a[id]);
if(flg)a[0].ins(dep[p]);
int p1,p2;
a[0].val(p1,p2);
int t = hsh.qy2(p1|((ull)p2<<32));
if(s[t].empty())a[t].get(a[0]);
ins(p,t);
}
inline void renew_dl(int id,int p,bool flg){
a[0].get(a[id]);
if(flg)a[0].ins(dep[p]);
int p1,p2;
a[0].val(p1,p2);
int t = hsh.qy2(p1|(((ull)p2)<<32));
del(p,t);
}
inline void ins(int x,int id){
ull pre = ((ull)id)<<32;
if(s[id].empty())ans.insert((node1){id});
if(!ctx.qy(pre|x,1)){
s[id].insert((node2){x,dfn[x]});
auto t = s[id].lower_bound((node2){x,dfn[x]});
int p1,p2;
p1 = p2 = 0;
if(next(t)!=s[id].end())p2 = (*next(t)).x;
if(t!=s[id].begin())p1 = (*prev(t)).x;
if(p1||p2){
if(!p1||!p2){
int p = lca(p1+p2,x);
if((p1?ok(p1,x):ok(x,p2))&&!ctp.qy(pre|p,1))renew_ad(id,p,1);
}
else{
int g1 = lca(p1,x),g2 = lca(p2,x),g3 = lca(p1,p2);
if(ok(p1,x)&&!ctp.qy(pre|g1,1))renew_ad(id,g1,1);
if(ok(x,p2)&&!ctp.qy(pre|g2,1))renew_ad(id,g2,1);
if(ok(p1,p2)&&!ctp.qy(pre|g3,0))renew_dl(id,g3,1);
}
}
}
}
inline void del(int x,int id){
ull pre = ((ull)id)<<32;
if(!ctx.qy(pre|x,0)){
auto t = s[id].lower_bound((node2){x,dfn[x]});
int p1,p2;
p1 = p2 = 0;
if(next(t)!=s[id].end())p2 = (*next(t)).x;
if(t!=s[id].begin())p1 = (*prev(t)).x;
if(p1||p2){
if(!p1||!p2){
int p = lca(p1+p2,x);
if((p1?ok(p1,x):ok(x,p2))&&!ctp.qy(pre|p,0))renew_dl(id,p,1);
}
else{
int g1 = lca(p1,x),g2 = lca(p2,x),g3 = lca(p1,p2);
bool flg1 = ok(p1,x)&&!ctp.qy(pre|g1,0);
bool flg2 = ok(x,p2)&&!ctp.qy(pre|g2,0);
bool flg3 = ok(p1,p2)&&!ctp.qy(pre|g3,1);
if(flg3)renew_ad(id,g3,1);
if(flg1)renew_dl(id,g1,1);
if(flg2)renew_dl(id,g2,1);
}
}
s[id].erase((node2){x,dfn[x]});
}
if(s[id].empty()){
ans.erase((node1){id});
int p1,p2;
a[id].val(p1,p2);
hsh.del(p1|(((ull)p2)<<32));
}
}
inline int qy(){
return ans.empty()?0:a[(*ans.begin()).x].cal();
}
inline void init_leaf(){
FOR(i,1,n)if(sz[i]==1&&v[i]){
int x = get_new();
a[2*n].init(v[i],dep[i]);
renew_ad(2*n,i,0);
}
}
signed main(){
init_pw(N-1);
//rd
readx(n);
FOR(i,2,n)readx(tf[i]),add(tf[i],i);
FOR(i,1,n)readx(v[i]);
//dfs0,init_st
dep[0] = -1;
dfs0(1,0),init_st(n);
init_leaf();
output(qy()),putchar(10);
readx(m);
int tx,tv;
// bool flg = 1;
FOR(i,1,m){
readx(tx),readx(tv);
int pre = v[tx];
if(tv){
a[2*n].init(tv,dep[tx]);
renew_ad(2*n,tx,0);
}
if(pre){
a[2*n].init(pre,dep[tx]);
renew_dl(2*n,tx,0);
}
v[tx] = tv;
output(qy()),putchar(10);
}
return 0;
}