stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <stdio.h>

#include <stdlib.h>

#include <cmath>

#include <string.h>

#include <iostream>

#include<algorithm>

#include <queue>

#include <vector>

#include<string>

//******************************************************

#define lrt (rt*2)

#define rrt  (rt*2+1)

#define LL long long

#define inf 0x3f3f3f3f

#define pi acos(-1.0)

#define exp 1e-8

const double temp=(1+sqrt(5))/2;

//***************************************************

#define eps             1e-8

#define inf             0x3f3f3f3f

#define INF             2e18

#define LL              long long

#define ULL             unsigned long long

#define PI              acos(-1.0)

#define pb              push_back

#define mk              make_pair



#define all(a)          a.begin(),a.end()

#define rall(a)         a.rbegin(),a.rend()

#define SQR(a)          ((a)*(a))

#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())

#define min3(a,b,c)     min(a,min(b,c))

#define max3(a,b,c)     max(a,max(b,c))

#define min4(a,b,c,d)   min(min(a,b),min(c,d))

#define max4(a,b,c,d)   max(max(a,b),max(c,d))

#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))

#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))

#define Iterator(a)     __typeof__(a.begin())

#define rIterator(a)    __typeof__(a.rbegin())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(0)

#define CasePrint       pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ')

#define vi              vector <int>

#define vL              vector <LL>

#define For(I,A,B)      for(int I = (A); I < (B); ++I)

#define rFor(I,A,B)     for(int I = (A); I >= (B); --I)

#define Rep(I,N)        For(I,0,N)

using namespace std;

const int maxn=1e5+10;

int pre[maxn];

void iint()

{

    for(int i=0;i<maxn;i++) pre[i]=i;

}

int find(int x)

{

    return x==pre[x]?x:find(pre[x]);

}

void join(int x,int y)

{

    int fx=find(x),fy=find(y);

    if(fx!=fy)  pre[fx]=fy;

}

int main()

{

    int n,k,num;

    char str[5];

    while(cin>>n>>k)

    {

        LL sum=0;

        if(n+k==0)  break;

        iint();

        for(int i=2;i<=n;i++)

        {

            scanf("%d",&pre[i]);

        }

        while(k--)

        {

            scanf("%s%d",str,&num);

            if(str[0]=='M') pre[num]=num;

            else if(str[0]=='Q')    sum+=find(num);

        }

        cout<<sum<<endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4196245867023576x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



int find(int x){ return (par[x]==x)?x:find(par[x]);}



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <string.h>

#include <vector>

#include <queue>

#include <functional>

#include <algorithm>

using namespace std;



const int MAXN = 100010;

const int MAXM = 100010;

const int INF = 0x3f3f3f3f;

typedef long long LL;

typedef pair<int, int> P;



int N, Q;

int father[MAXN];

bool vis[MAXN];



void Init() {

	for (int i = 0; i < MAXN; ++i) {

		father[i] = i;

	}

}

int Find(int x) {

	while (!vis[x])

		x = father[x];

	return x;

}



int main(int argc, char const *argv[]) {

#ifdef LOCAL

	freopen("input.txt", "r", stdin);

	freopen("output.txt", "w", stdout);

#endif

	while (scanf("%d%d", &N, &Q) != EOF && !(N == 0 && Q == 0)) {

		int a, b;

		LL res = 0;

		char opt[5];

		Init();

		fill(vis, vis + MAXN, false);

		vis[1] = true;

		for (int i = 2; i <= N; ++i) {

			scanf("%d", &father[i]);

		}

		for (int i = 0; i < Q; ++i) {

			scanf("%s %d", opt, &b);

			if (opt[0] == 'Q') {

				res += Find(b);

			}

			else

				vis[b] = true;

		}

		printf("%lld\n", res);

	}

	//system("pause");

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.5059782190946824x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



int find(int x){ return (par[x]==x)?x:find(par[x]);}



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <string.h>

#include <vector>

#include <queue>

#include <functional>

#include <algorithm>

using namespace std;



const int MAXN = 100010;

const int MAXM = 100010;

const int INF = 0x3f3f3f3f;

typedef long long LL;

typedef pair<int, int> P;



int N, Q;

int father[MAXN];

bool vis[MAXN];



void Init() {

	for (int i = 0; i < MAXN; ++i) {

		father[i] = i;

	}

}

int Find(int x) {

	if (father[x] == x)

		return x;

	return Find(father[x]);

}



int main(int argc, char const *argv[]) {

#ifdef LOCAL

	freopen("input.txt", "r", stdin);

	freopen("output.txt", "w", stdout);

#endif

	while (scanf("%d%d", &N, &Q) != EOF && !(N == 0 && Q == 0)) {

		int a, b;

		LL res = 0;

		char opt[5];

		Init();

		for (int i = 2; i <= N; ++i) {

			scanf("%d", &father[i]);

		}

		for (int i = 0; i < Q; ++i) {

			scanf("%s %d", opt, &b);

			if (opt[0] == 'Q') {

				res += Find(b);

			}

			else

				father[b] = b;

		}

		printf("%lld\n", res);

	}

	//system("pause");

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.4048057277801127x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



int find(int x){ return (par[x]==x)?x:find(par[x]);}



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <string.h>

#include <vector>

#include <queue>

#include <stack>

#include <functional>

#include <algorithm>

using namespace std;



const int MAXN = 100010;

const int MAXM = 100010;

const int INF = 0x3f3f3f3f;

typedef long long LL;

typedef pair<char, int> P;



int N, Q;

int father[MAXN]; // 并查集中的父节点

int parent[MAXN]; // 树中的父节点

int vis[MAXN]; // 结点标记次数

vector<int> e[MAXN]; // 记录边，即树的结构e[i][j]表示i的第j+1孩子

stack<P> st; // 存储操作



void dfs(int i, int f = 1) {

	if (vis[i])

		f = i;

	father[i] = f;

	vector<int> &ei = e[i];

	for (vector<int>::iterator it = ei.begin(); it != ei.end(); ++it) {

		dfs(*it, f);

	}

}



//void Init() {

//	for (int i = 0; i < MAXN; ++i) {

//		father[i] = i;

//	}

//}

int Find(int x) {

	if (father[x] == x)

		return x;

	return father[x] = Find(father[x]);

}



int main(int argc, char const *argv[]) {

#ifdef LOCAL

	freopen("input.txt", "r", stdin);

	freopen("output.txt", "w", stdout);

#endif

	while (scanf("%d%d", &N, &Q) != EOF && !(N == 0 && Q == 0)) {

		int i, j, q;

		LL res = 0;

		char opt[5];

		for (i = 1; i <= N; ++i) {

			e[i].clear();

		}

		fill(vis, vis + MAXN, 0);

		vis[1] = 1;

		for (i = 2; i <= N; ++i) {

			scanf("%d", &j);

			e[j].push_back(i);// 结点j的孩子i,用于dfs

			parent[i] = j; // 结点i的父亲j

		}

		for (int i = 0; i < Q; ++i) {

			scanf("%s %d", opt, &j);

			if (opt[0] == 'M') {

				++vis[j];// 结点j标记次数

			}

			st.push(P(opt[0], j));// 将操作入栈，操作名和结点

		}

		dfs(1); // 寻找最近染色祖先

		// 逆序处理操作

		while (!st.empty()) {

			i = st.top().second; // 被操作结点

			if (st.top().first == 'M') { 

				if (--vis[i] == 0) // 直到多余标记结束

					father[i] = Find(parent[i]); // 取消染色

			} else {

				res += Find(i); // 查询操作，返回最近染色祖先

			}

			st.pop(); // 处理下一操作

		}

		printf("%lld\n", res);

	}

	//system("pause");

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 3.9774547332988934x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



int find(int x){ return (par[x]==x)?x:find(par[x]);}



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
/* 

 author:hdsdogge 

 begin: 

 end: 

 cost: 

 */

#include<iostream>

#include<string>

#include<queue>

#include<map>

#include<stack>

#include<algorithm>

#include<cmath>

#include<cstdio>

#include<cstring>

#include<vector>

#include<bitset>

#include<cstdlib>

#include<list>

#include <sstream>

#include<ctype.h>

using namespace std;

const int maxn=1000000+10;

typedef pair<int,int> P;

typedef long long ll;

const double PI = acos(-1.0);

const double eps = 1e-6;

const int INF = 1000000000;

int T,n,m;

int par[maxn];

int vis[maxn];

int main() {

   // freopen("test","r",stdin);

   // freopen("out","w",stdout);

    std::ios::sync_with_stdio(false);

    std::cin.tie(0);

    while(cin>>n>>m&&(n+m)){

        memset(vis,0, sizeof(vis));

        par[1]=1;

        vis[1]=1;

        ll ans=0;

        for(int i=2;i<=n;i++){

            int x;

            cin>>x;

            par[i]=x;

        }

        for(int i=0;i<m;i++){

            char c;

            int x;

            cin>>c>>x;

            if(c=='Q'){

                while(vis[x]==0){

                    x=par[x];

                }

                ans+=x;

            }

            else if(c=='M'){

               vis[x]=1;

            }

        }

        cout<<ans<<endl;

    }

    return 0;

} 






// Faster C++ solution for the same problem (measured speedup: 5.1493508932276635x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



int find(int x){ return (par[x]==x)?x:find(par[x]);}



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <sstream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <iomanip>

#include <utility>

#include <string>

#include <cmath>

#include <vector>

#include <bitset>

#include <stack>

#include <queue>

#include <deque>

#include <map>

#include <set>



using namespace std;



typedef long long ll;

typedef unsigned long long ull;

const int dir[9][2] = { 0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1, 0, 0 };

const ll  ll_inf = 0x7fffffff;

const double PI=acos(-1.0);

template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

const int inf = 0x3f3f3f;

const int mod = (int) 998244353;

const int Max = (int) 1e5 + 22;

stringstream ss;



/**********************************************Head-----Template****************************************/

bool Finish_read;

template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}

template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}

template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}

template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}



/********************************Head----Temlate**********************************************/



//tools:

//priority_queue <int, vector<int>, greater<int> > Q;

//ios::sync_with_stdio(false);

//freopen("input.txt", "r", stdin);

//ss<<int; ss>>str;



int n, q;

int Fa[Max];

int value[Max];



void Init() {

	Fa[1] = 0;

	memset(value, 0, sizeof(value));

	value[1] = 1;

}



int main() {

	while (~scanf("%d %d", &n, &q) && n+q) {

		Init();

		for (int i = 2; i <= n; ++i) {

			scanf("%d", &Fa[i]);

		}



		ll ans = 0;

		int x;

		char ch[10];

		for (int i = 0; i < q; ++i) {

			scanf("%s %d", ch, &x);

			if (ch[0] == 'Q') {

				while (x) {

					if (value[x]) {

						ans += (ll)x;

						break;

					}

					x = Fa[x];

				}

			}

			else {

				value[x] = 1;

			}

		}



		printf("%lld\n", ans);

	}

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.4154226874836573x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



int find(int x){ return (par[x]==x)?x:find(par[x]);}



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

int main() {

    int N, Q;

    int mark[100001];

    int tree[100001];

    while (1) {

        cin >> N >> Q;

        if (N == 0 && Q == 0) {

            break;

        }



        fill_n(mark, 100001, 0);

        mark[1] = 1; 

        tree[1] = 0;

        for (int i=2; i<=N; ++i) {

            cin >> tree[i];

        } 



        long long sum = 0;

        for (int i=0; i<Q; ++i) {

            char ope;

            int v;

            cin >> ope >> v;

            if (ope == 'Q') {

                for (int j=v; j>=1; j=tree[j]) {

                    if (mark[j] == 1) {

                        sum += j;

                        break;

                    }

                }

            } else if (ope == 'M') {

                mark[v] = 1;

            }

        }

        cout << sum << endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2673321867682865x):
#include <iostream>

#include <cstring>

using namespace std;

int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);



    int N, Q;

    bool mark[100001];

    int tree[100001];

    while (1) {

        cin >> N >> Q;

        if (N == 0 && Q == 0) {

            break;

        }



        memset(mark, false, sizeof(mark));

        mark[1] = true; 

        tree[1] = 0;

        for (int i=2; i<=N; ++i) {

            cin >> tree[i];

        } 



        long long sum = 0;

        for (int i=0; i<Q; ++i) {

            char ope;

            int v;

            cin >> ope >> v;

            if (ope == 'Q') {

                for (int j=v; j>=1; j=tree[j]) {

                    if (mark[j] == true) {

                        sum += j;

                        break;

                    }

                }

            } else if (ope == 'M') {

                mark[v] = true;

            }

        }

        cout << sum << "\n";

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

using namespace std;



const int N = 100000;



int n, p;

int pre[N+1];



int find(int x)

{

    while (x != pre[x])

        x = pre[x];

    return x;

}



int main(void)

{

    while (scanf("%d%d", &n, &p) != EOF) {

        if (!n && !p) break;

        pre[1] = 1;

        for (int i = 2; i <= n; i ++)

            scanf("%d", &pre[i]);

        long long sum = 0;

        for (int i = 1; i <= p; i ++) {

            char s[2];

            int x;

            scanf("%s%d", s, &x);

            if (s[0] == 'M')

                pre[x] = x;

            else

                sum += find(x);

        }

        printf("%lld\n", sum);

    }



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.16791442891752x):
#include <cstdio>

#include <cstring>

#include <queue>

using namespace std;

#define ll long long

int q, n, v;

char c;

int p[100005];

int main() {

    while ((~scanf("%d %d", &n, &q)) && q && n) {

        for (int i = 2; i <= n; ++i) scanf("%d", &p[i]);

        p[1] = 1;

        ll ans = 0;

        for (int i = 0; i < q; ++i) {

            scanf("\n%c %d", &c, &v);

            if (c == 'M') p[v] = v;

            else {

                while (p[v] != v) v = p[v];

                ans += v;

            }

        }

        printf("%lld\n", ans);

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



#define MAX_N 100001



typedef unsigned long long ull;



bool M[MAX_N];   // マークされてるかどうか

int P[MAX_N];   // 親



int main() {

    int N, Q;

    while (cin >> N >> Q, N != 0 && Q != 0) {

        memset(M, 0, sizeof(M));

        M[1] = true;

        memset(P, 0, sizeof(P));

        for (int i = 2; i < N+1; i++) {

            int p; cin >> p;

            P[i] = p;

        }



//        for (int i = 1; i <= N; i++) cout << P[i] << ' ';

//        cout << endl;

//        for (int i = 1; i <= N; i++) cout << M[i] << ' ';

//        cout << endl;



        ull sum = 0;

        for (int i = 0; i < Q; i++) {

            char q; int v;

            cin >> q >> v;

            if (q == 'Q') {

                while (!M[v]) {

                    v = P[v];

                }

                sum += v;

            } else {

                M[v] = true;

            }

        }

//        for (int i = 1; i <= N; i++) cout << M[i] << ' ';

//        cout << endl;

        cout << sum << endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.5031265005287766x):
/* UnionFindの練習問題の項にあったのでそれっぽくやってみた

 * ぶっちゃけ愚直に実装したほうがいい

 */

#include <cstdio>

#include <cassert>

#include <cstring>

#include <iomanip>

#include <functional>

#include <algorithm>

#include <iostream>

#include <vector>

#include <map>

#include <iterator>

#include <queue>

#include <utility>

#include <stack>



using namespace std;



typedef long long Long;



#define MN 100000

#define MQ 100000

int N, Q;

int parent[MN + 1];

bool marked[MN + 1];

pair<int, int> queries[MQ];

int group[MN + 1];



int root(int x) {

    if (marked[x]) return x;

    return parent[x] = root(parent[x]);

}

void merge(int x, int y) {

    x = root(x); y = root(y);

    if (x == y) return;

    parent[x] = y;

}



bool input() {

    scanf("%d %d", &N, &Q);

    if (N == 0 && Q == 0) return false;

    for (int i = 2; i <= N; i++) {

        int p;

        scanf("%d", &p);

        parent[i] = p;

    }

    memset(marked, false, N + 1);

    marked[1] = true;

    for (int i = 0; i < Q; i++) {

        char t; int n;

        scanf("%*[ \t\r\n]%c %d", &t, &n);

        if (t == 'M') {

            if (marked[n]) {

                queries[i] = make_pair('X', 0);

                continue;

            }

            marked[n] = true;

        }

        queries[i] = make_pair(t, n);

    }

    return true;

}



void solve() {

    Long ans = 0;

    for (int q = Q - 1; q >= 0; q--) {

        pair<int, int> query = queries[q];

        if (query.first == 'Q') {

            ans += root(query.second);

        } else if (query.first == 'M') {

            marked[query.second] = false;

        }

    }

    printf("%lld\n", ans);

}



int main() {

    while (input()) {

        solve();

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;



#define MAX_N 100001



typedef unsigned long long ull;



bool M[MAX_N];   // マークされてるかどうか

int P[MAX_N];   // 親



int main() {

    int N, Q;

    while (cin >> N >> Q, N != 0 && Q != 0) {

        memset(M, 0, sizeof(M));

        M[1] = true;

        memset(P, 0, sizeof(P));

        for (int i = 2; i < N+1; i++) {

            int p; cin >> p;

            P[i] = p;

        }



//        for (int i = 1; i <= N; i++) cout << P[i] << ' ';

//        cout << endl;

//        for (int i = 1; i <= N; i++) cout << M[i] << ' ';

//        cout << endl;



        ull sum = 0;

        for (int i = 0; i < Q; i++) {

            char q; int v;

            cin >> q >> v;

            if (q == 'Q') {

                while (!M[v]) {

                    v = P[v];

                }

                sum += v;

            } else {

                M[v] = true;

            }

        }

//        for (int i = 1; i <= N; i++) cout << M[i] << ' ';

//        cout << endl;

        cout << sum << endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.5150067601020936x):
#include <cstdio>

#include <cassert>

#include <cstring>

#include <iomanip>

#include <functional>

#include <algorithm>

#include <iostream>

#include <vector>

#include <map>

#include <iterator>

#include <queue>

#include <utility>

#include <stack>



using namespace std;



typedef long long Long;



#define MN 100000

#define MQ 100000

int N, Q;

int parent[MN + 1];

bool marked[MN + 1];

pair<int, int> queries[MQ];



int root(int x) {

    if (marked[x]) return x;

    return parent[x] = root(parent[x]);

}

void merge(int x, int y) {

    x = root(x); y = root(y);

    if (x == y) return;

    parent[x] = y;

}



bool input() {

    scanf("%d %d", &N, &Q);

    if (N == 0 && Q == 0) return false;

    for (int i = 2; i <= N; i++) {

        int p;

        scanf("%d", &p);

        parent[i] = p;

    }

    memset(marked, false, N + 1);

    marked[1] = true;

    for (int i = 0; i < Q; i++) {

        char t; int n;

        scanf("%*[ \t\r\n]%c %d", &t, &n);

        if (t == 'M') {

            if (marked[n]) {

                queries[i] = make_pair('X', 0);

                continue;

            }

            marked[n] = true;

        }

        queries[i] = make_pair(t, n);

    }

    return true;

}



void solve() {

    Long ans = 0;

    for (int q = Q - 1; q >= 0; q--) {

        pair<int, int> query = queries[q];

        if (query.first == 'Q') {

            ans += root(query.second);

        } else if (query.first == 'M') {

            marked[query.second] = false;

        }

    }

    printf("%lld\n", ans);

}



int main() {

    while (input()) {

        solve();

    }

    return 0;

}