#include<bits/stdc++.h>
#define N 100050
#define int long long
using namespace std;
int first[N], nxt[N], to[N], tot;
void add(int x,int y){ nxt[++tot] = first[x], first[x] = tot, to[tot] = y;}
int a[N],n,m,Root;
int siz[N],fa[N],dep[N],son[N],top[N],id[N],sign;
int read(){
int cnt=0; char ch=0;
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
return cnt;
}
struct Segmentree{
int sum[N<<2], tag[N<<2];
void Pushup(int x){ sum[x] = sum[x<<1] + sum[x<<1|1];}
void Pushdown(int x,int l,int r){
if(tag[x]){
int mid = (l+r) >> 1;
sum[x<<1] += (mid-l+1) * tag[x];
sum[x<<1|1] += (r-mid) * tag[x];
tag[x<<1] += tag[x]; tag[x<<1|1] += tag[x];
tag[x] = 0;
}
}
void Modify(int x,int l,int r,int L,int R,int val){
if(L<=l && r<=R){
sum[x] += (r-l+1) * val;
tag[x] += val;
return;
}
Pushdown(x,l,r);
int mid = (l+r) >> 1;
if(L<=mid) Modify(x<<1,l,mid,L,R,val);
if(R>mid) Modify(x<<1|1,mid+1,r,L,R,val);
Pushup(x);
}
int Quary(int x,int l,int r,int L,int R){
if(L<=l && r<=R) return sum[x];
Pushdown(x,l,r);
int mid = (l+r) >> 1, ans = 0;
if(L<=mid) ans += Quary(x<<1,l,mid,L,R);
if(R>mid) ans += Quary(x<<1|1,mid+1,r,L,R);
return ans;
}
}Seg;
void dfs(int u,int f){
siz[u] = 1;
for(int i=first[u];i;i=nxt[i]){
int t=to[i];
dep[t] = dep[u] + 1, fa[t] = u;
dfs(t,u); siz[u] += siz[t];
if(siz[son[u]] < siz[t]) son[u] = t;
}
}
void dfs2(int u,int Top){
id[u] = ++sign; top[u] = Top;
Seg.Modify(1,1,n,id[u],id[u],a[u]);
if(son[u]) dfs2(son[u], Top);
for(int i=first[u];i;i=nxt[i]){
int t=to[i]; if(t==son[u]) continue;
dfs2(t,t);
}
}
int LCA(int x,int y){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
x = fa[top[x]];
} if(dep[x] > dep[y]) swap(x,y); return x;
}
int find(int now,int goal){
while(top[now] != top[goal]){
if(dep[top[now]] < dep[top[goal]]) swap(now, goal);
if(fa[top[now]] == goal) return top[now];
now = fa[top[now]];
}
if(dep[now] > dep[goal]) swap(now, goal);
return son[now];
}
void Road_Modify(int x,int y,int w){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
Seg.Modify(1,1,n,id[top[x]],id[x],w);
x = fa[top[x]];
}
if(id[x] > id[y]) swap(x,y);
Seg.Modify(1,1,n,id[x],id[y],w);
}
void Son_Modify(int x,int w){
if(x == Root){ Seg.Modify(1,1,n,1,n,w); return;}
int lca = LCA(x, Root);
if(lca != x){ Seg.Modify(1,1,n, id[x], id[x]+siz[x]-1, w); return;}
int son = find(Root, lca);
Seg.Modify(1,1,n, 1,n,w);
Seg.Modify(1,1,n, id[son], id[son]+siz[son]-1, -w);
}
int Road_Quary(int x,int y){
int ans = 0;
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
ans += Seg.Quary(1,1,n,id[top[x]],id[x]);
x = fa[top[x]];
}
if(id[x] > id[y]) swap(x,y);
return ans + Seg.Quary(1,1,n,id[x],id[y]);
}
int Son_Quary(int x){
if(x == Root) return Seg.Quary(1,1,n,1,n);
int lca = LCA(x, Root);
if(lca != x) return Seg.Quary(1,1,n, id[x], id[x]+siz[x]-1);
int son = find(Root, lca);
return Seg.Quary(1,1,n,1,n) - Seg.Quary(1,1,n,id[son],id[son]+siz[son]-1);
}
signed main(){
n = read();
for(int i=1;i<=n;i++) a[i] = read();
for(int i=2;i<=n;i++) add(read(), i);
dfs(1,0); dfs2(1,1); m = read();
while(m--){
int op = read();
if(op==1){ Root = read();}
if(op==2){ int u = read(), v = read(), w = read(); Road_Modify(u, v, w);}
if(op==3){ int u = read(), w = read(); Son_Modify(u, w);}
if(op==4){ int u = read(), v = read(); printf("%lld\n",Road_Quary(u,v));}
if(op==5){ int u = read(); printf("%lld\n",Son_Quary(u));}
} return 0;
}