Problem: Oscolcovo city has a campus consisting of n student dormitories, n universities and n military offices. Initially, the i-th dormitory belongs to the i-th university and is assigned to the i-th military office.

Life goes on and the campus is continuously going through some changes. The changes can be of four types:

  1. University aj merges with university bj. After that all the dormitories that belonged to university bj are assigned to to university aj, and university bj disappears. 
  2. Military office cj merges with military office dj. After that all the dormitories that were assigned to military office dj, are assigned to military office cj, and military office dj disappears. 
  3. Students of university xj move in dormitories. Lets kxj is the number of dormitories that belong to this university at the time when the students move in. Then the number of students in each dormitory of university xj increases by kxj (note that the more dormitories belong to the university, the more students move in each dormitory of the university). 
  4. Military office number yj conducts raids on all the dormitories assigned to it and takes all students from there. 



Thus, at each moment of time each dormitory is assigned to exactly one university and one military office. Initially, all the dormitory are empty.

Your task is to process the changes that take place in the campus and answer the queries, how many people currently live in dormitory qj.

Input

The first line contains two integers, n and m (1 ≤ n, m ≤ 5·105) — the number of dormitories and the number of queries, respectively.

Next m lines contain the queries, each of them is given in one of the following formats: 

  * «U aj bj» — merging universities; 
  * «M cj dj» — merging military offices; 
  * «A xj» — students of university xj moving in the dormitories; 
  * «Z yj» — a raid in military office yj; 
  * «Q qj» — a query asking the number of people in dormitory qj. 

All the numbers in the queries are positive integers and do not exceed n. It is guaranteed that at the moment of the query the universities and military offices, that are present in the query, exist.

Output

In the i-th line print the answer to the i-th query asking the number of people in the dormitory.

Examples

Input

2 7
A 1
Q 1
U 1 2
A 1
Z 1
Q 1
Q 2


Output

1
0
2


Input

5 12
U 1 2
M 4 5
A 1
Q 1
A 3
A 4
Q 3
Q 4
Z 4
Q 4
A 5
Q 5


Output

2
1
1
0
1

Note

Consider the first sample test: 

  * In the first query university 1 owns only dormitory 1, so after the query dormitory 1 will have 1 student. 
  * After the third query university 1 owns dormitories 1 and 2. 
  * The fourth query increases by 2 the number of students living in dormitories 1 and 2 that belong to university number 1. After that 3 students live in the first dormitory and 2 students live in the second dormitory. 
  * At the fifth query the number of students living in dormitory 1, assigned to the military office 1, becomes zero. 

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
long long int powm(long long int a, int b, int n) {
  long long int rm = 1;
  while (b) {
    if (b % 2) {
      rm = (rm * a) % n;
    }
    a = (a * a) % n;
    b /= 2;
  }
  return rm;
}
int a_office[int(5e5 + 1000)], t_office = 0, a_university[int(5e5 + 1000)],
                               t_university = 0, lo[int(5e5 + 1000)],
                               ro[int(5e5 + 1000)], lu[int(5e5 + 1000)],
                               ru[int(5e5 + 1000)];
vector<int> university_version[4 * int(5e5 + 1000)];
vector<long long int> university_persistant[4 * int(5e5 + 1000)];
int ln[4 * int(5e5 + 1000)], rn[4 * int(5e5 + 1000)],
    office_seg[4 * int(5e5 + 1000)] = {0};
char type[int(5e5 + 1000)][3];
int q1[int(5e5 + 1000)], q2[int(5e5 + 1000)], activated_office[int(5e5 + 1000)],
    activated_university[int(5e5 + 1000)];
bool root_office[int(5e5 + 1000)], root_university[int(5e5 + 1000)];
vector<int> office_edge[int(5e5 + 1000)], university_edge[int(5e5 + 1000)];
long long int answer;
void dfs1(int vertex) {
  a_office[++t_office] = vertex;
  lo[vertex] = t_office;
  for (int child : office_edge[vertex]) {
    dfs1(child);
  }
  ro[vertex] = t_office;
}
void dfs2(int vertex) {
  a_university[++t_university] = vertex;
  lu[vertex] = t_university;
  for (int child : university_edge[vertex]) {
    dfs2(child);
  }
  ru[vertex] = t_university;
}
void university_update(int n, int it, int f, int from, int to, int Val,
                       int version) {
  if (it == from && f == to) {
    int szz = university_version[n].size();
    if (!szz)
      university_persistant[n].push_back(Val);
    else {
      university_persistant[n].push_back(Val +
                                         university_persistant[n][szz - 1]);
    }
    university_version[n].push_back(version);
    return;
  }
  int m = (it + f) / 2;
  if (to <= m)
    university_update(ln[n], it, m, from, to, Val, version);
  else if (from > m)
    university_update(rn[n], m + 1, f, from, to, Val, version);
  else {
    university_update(ln[n], it, m, from, m, Val, version);
    university_update(rn[n], m + 1, f, m + 1, to, Val, version);
  }
}
void office_update(int n, int it, int f, int from, int to, int version) {
  if (it == from && f == to) {
    office_seg[n] = version;
    return;
  }
  int m = (it + f) / 2;
  if (to <= m)
    office_update(ln[n], it, m, from, to, version);
  else if (from > m)
    office_update(rn[n], m + 1, f, from, to, version);
  else {
    office_update(ln[n], it, m, from, m, version);
    office_update(rn[n], m + 1, f, m + 1, to, version);
  }
}
int office_query(int n, int it, int f, int q, int max_version) {
  max_version = max(max_version, office_seg[n]);
  if (it == f) return max_version;
  int m = (it + f) / 2;
  if (q <= m)
    return office_query(ln[n], it, m, q, max_version);
  else
    return office_query(rn[n], m + 1, f, q, max_version);
}
void university_query(int n, int it, int f, int q, int version) {
  int szz2 = university_version[n].size();
  int from = upper_bound(university_version[n].begin(),
                         university_version[n].end(), version) -
             university_version[n].begin();
  if (from < szz2) {
    answer += university_persistant[n][szz2 - 1];
    if (from > 0) answer -= university_persistant[n][from - 1];
  }
  if (it == f) return;
  int m = (it + f) / 2;
  if (q <= m)
    university_query(ln[n], it, m, q, version);
  else
    university_query(rn[n], m + 1, f, q, version);
}
int main() {
  memset(activated_office, -1, sizeof(activated_office));
  ;
  memset(activated_university, -1, sizeof(activated_university));
  ;
  int n, m;
  scanf("%d", &n);
  ;
  scanf("%d", &m);
  ;
  for (int i = int(1); i <= (int)n; i++)
    root_office[i] = root_university[i] = true;
  for (int i = int(1); i <= (int)m; i++) {
    scanf("%s", type[i]);
    scanf("%d", &q1[i]);
    ;
    if (type[i][0] == 'U' || type[i][0] == 'M') scanf("%d", &q2[i]);
    ;
    if (type[i][0] == 'U') {
      root_university[q2[i]] = false;
      university_edge[q1[i]].push_back(q2[i]);
    } else if (type[i][0] == 'M') {
      root_office[q2[i]] = false;
      office_edge[q1[i]].push_back(q2[i]);
    }
  }
  for (int i = 1; i <= 4 * 500000; ++i) ln[i] = i * 2, rn[i] = i * 2 + 1;
  for (int i = int(1); i <= (int)n; i++) {
    if (root_office[i]) {
      dfs1(i);
    }
  }
  for (int i = int(1); i <= (int)n; i++) {
    if (root_university[i]) {
      dfs2(i);
    }
  }
  int AddValue, from, to, next_child, max_version;
  for (int i = int(1); i <= (int)m; i++) {
    if (type[i][0] == 'U') {
      activated_university[q1[i]]++;
    } else if (type[i][0] == 'M') {
      activated_office[q1[i]]++;
    } else if (type[i][0] == 'A') {
      if (activated_university[q1[i]] == -1) {
        from = to = lu[q1[i]];
      } else {
        from = lu[q1[i]];
        next_child = university_edge[q1[i]][activated_university[q1[i]]];
        to = ru[next_child];
      }
      AddValue = to - from + 1;
      university_update(1, 1, n, from, to, AddValue, i);
    } else if (type[i][0] == 'Z') {
      if (activated_office[q1[i]] == -1) {
        from = to = lo[q1[i]];
      } else {
        from = lo[q1[i]];
        next_child = office_edge[q1[i]][activated_office[q1[i]]];
        to = ro[next_child];
      }
      office_update(1, 1, n, from, to, i);
    } else {
      answer = 0;
      max_version = office_query(1, 1, n, lo[q1[i]], 0);
      university_query(1, 1, n, lu[q1[i]], max_version);
      printf("%I64d\n", answer);
    }
  }
  return 0;
}