/*
ye mera template hai
apna khud likho bc :P
*/
/*
Author : Sarvagya Agarwal
*/
#include<bits/stdc++.h>
using namespace std;
//defines
#define openin freopen("input.txt","r",stdin)
#define openout freopen("output.txt","w",stdout)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define int long long
#define mod 1000000007
#define rep(i,x,y) for (__typeof(x) i=x;i<=y;i++)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
int power(int a , int b)
{
int res = 1 ;
while(b)
{
if(b%2) {
res = (res * a)%mod ;
}
b/=2 ;
a = (a*a)%mod ;
}
return res ;
}
//debug
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const int MAXN = 1e5+5 ;
int sum = 0 , n , sz[MAXN] , weight[MAXN] ,parent[MAXN];
vector<int> graph[MAXN] ;
set<int> tree[MAXN] ;
int st[MAXN] , en[MAXN] ;
int gt = 1 ;
void dfs(int node = 1)
{
st[node] = gt++ ;
sz[node] = weight[node] ;
tree[node].insert(node) ;
for(auto child : graph[node]) {
if(child != parent[node]) {
dfs(child) ;
sz[node] += sz[child] ;
tree[node].insert(all(tree[child])) ;
}
}
en[node] = gt++ ;
}
bool is_ancestor(int Y,int X)
{
return st[Y] < st[X] && en[Y] > st[X] ;
}
int32_t main()
{
fast;
cin >> n ;
for(int i = 1 ; i <= n ; ++i) {
cin >> weight[i] ;
sum += weight[i] ;
}
parent[1] = 0 ;
for(int i = 2 ; i <= n ; ++i) {
cin >> parent[i] ;
graph[parent[i]].emplace_back(i) ;
graph[i].emplace_back(parent[i]) ;
}
dfs() ;
int q ;
cin >> q ;
int X = 1 ;
while(q--)
{
char c ;
cin >> c ;
if(c=='S') {
int Y ;
cin >> Y ;
if(X==Y) {
cout << sum << '\n' ;
}
else if(is_ancestor(Y,X)) {
for(auto &child : graph[Y]) {
if(tree[child].find(X) != tree[child].end()) {
cout << sum - sz[child] << '\n' ;
break ;
}
}
}
else {
cout << sz[Y] << '\n' ;
}
}
else {
int root ;
cin >> root ;
X = root ;
}
}
return 0;
}
LyoKeWUgbWVyYSB0ZW1wbGF0ZSBoYWkKYXBuYSBraHVkIGxpa2hvIGJjIDpQCiovCgovKgpBdXRob3IgOiBTYXJ2YWd5YSBBZ2Fyd2FsCiovCgojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy9kZWZpbmVzCiNkZWZpbmUgb3BlbmluIGZyZW9wZW4oImlucHV0LnR4dCIsInIiLHN0ZGluKQojZGVmaW5lIG9wZW5vdXQgZnJlb3Blbigib3V0cHV0LnR4dCIsInciLHN0ZG91dCkKI2RlZmluZSBmYXN0IGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpO2Npbi50aWUoMCk7Y291dC50aWUoMCkKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKI2RlZmluZSByZXAoaSx4LHkpIGZvciAoX190eXBlb2YoeCkgaT14O2k8PXk7aSsrKQojZGVmaW5lIGFsbChjKSAoYykuYmVnaW4oKSwoYykuZW5kKCkKI2RlZmluZSBmZiBmaXJzdAojZGVmaW5lIHNzIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgoKaW50IHBvd2VyKGludCBhICwgaW50IGIpCnsKICAgIGludCByZXMgPSAxIDsKICAgIHdoaWxlKGIpCiAgICB7CiAgICAgICAgaWYoYiUyKSB7CiAgICAgICAgICAgIHJlcyA9ICh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XS5pbnNlcnQobm9kZSkgOwogICAgZm9yKGF1dG8gY2hpbGQgOiBncmFwaFtub2RlXSkgewogICAgICAgIGlmKGNoaWxkICE9IHBhcmVudFtub2RlXSkgewogICAgICAgICAgICBkZnMoY2hpbGQpIDsKICAgICAgICAgICAgc3pbbm9kZV0gKz0gc3pbY2hpbGRdIDsKICAgICAgICAgICAgdHJlZVtub2RlXS5pbnNlcnQoYWxsKHRyZWVbY2hpbGRdKSkgOwogICAgICAgIH0KICAgIH0KICAgIGVuW25vZGVdID0gZ3QrKyA7Cn0KYm9vbCBpc19hbmNlc3RvcihpbnQgWSxpbnQgWCkKewogICAgcmV0dXJuIHN0W1ldIDwgc3RbWF0gJiYgZW5bWV0gPiBzdFtYXSA7Cn0KaW50MzJfdCBtYWluKCkKewogICAgZmFzdDsKICAgIGNpbiA+PiBuIDsKICAgIGZvcihpbnQgaSA9IDEgOyBpIDw9IG4gOyArK2kpIHsKICAgICAgICBjaW4gPj4gd2VpZ2h0W2ldIDsKICAgICAgICBzdW0gKz0gd2VpZ2h0W2ldIDsKICAgIH0KICAgIHBhcmVudFsxXSA9IDAgOwogICAgZm9yKGludCBpID0gMiA7IGkgPD0gbiA7ICsraSkgewogICAgICAgIGNpbiA+PiBwYXJlbnRbaV0gOwogICAgICAgIGdyYXBoW3BhcmVudFtpXV0uZW1wbGFjZV9iYWNrKGkpIDsKICAgICAgICBncmFwaFtpXS5lbXBsYWNlX2JhY2socGFyZW50W2ldKSA7CiAgICB9CiAgICBkZnMoKSA7CiAgICBpbnQgcSA7CiAgICBjaW4gPj4gcSA7CiAgICBpbnQgWCA9IDEgOwogICAgd2hpbGUocS0tKQogICAgewogICAgICAgIGNoYXIgYyA7CiAgICAgICAgY2luID4+IGMgOwogICAgICAgIGlmKGM9PSdTJykgewogICAgICAgICAgICBpbnQgWSA7CiAgICAgICAgICAgIGNpbiA+PiBZIDsKICAgICAgICAgICAgaWYoWD09WSkgewogICAgICAgICAgICAgICAgY291dCA8PCBzdW0gPDwgJ1xuJyA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZihpc19hbmNlc3RvcihZLFgpKSB7CiAgICAgICAgICAgICAgICBmb3IoYXV0byAmY2hpbGQgOiBncmFwaFtZXSkgewogICAgICAgICAgICAgICAgICAgIGlmKHRyZWVbY2hpbGRdLmZpbmQoWCkgIT0gdHJlZVtjaGlsZF0uZW5kKCkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCBzdW0gLSBzeltjaGlsZF0gPDwgJ1xuJyA7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICBjb3V0IDw8IHN6W1ldIDw8ICdcbicgOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGVsc2UgewogICAgICAgICAgICBpbnQgcm9vdCA7CiAgICAgICAgICAgIGNpbiA+PiByb290IDsKICAgICAgICAgICAgWCA9IHJvb3QgOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==