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;
const int MAXN = int(5e5 + 100);
vector<int> mg[MAXN];
int mlpos[MAXN], mrpos[MAXN];
int mtin[MAXN];
int mendtime[MAXN];
char s[100];
struct query {
  char type;
  int a, b;
};
void mdfs(int v, int par, int& timer) {
  if (v == par) {
    ++timer;
    mtin[v] = timer;
    return;
  }
  for (auto to : mg[v]) mdfs(to, v, timer);
}
vector<int> mt[4 * MAXN];
void mt_add(int tv, int tl, int tr, int l, int r, int time) {
  if (l > r) return;
  if (tl == l && tr == r) {
    mt[tv].push_back(time);
    return;
  }
  int tm = (tl + tr) / 2;
  mt_add(2 * tv, tl, tm, l, min(tm, r), time);
  mt_add(2 * tv + 1, tm + 1, tr, max(tm + 1, l), r, time);
}
int mt_last(int tv, int tl, int tr, int pos, int ub) {
  auto it = upper_bound(mt[tv].begin(), mt[tv].end(), ub);
  int res = it == mt[tv].begin() ? 0 : *(--it);
  if (tl == tr) return res;
  int tm = (tl + tr) / 2;
  int sub_res = 0;
  if (pos <= tm)
    sub_res = mt_last(2 * tv, tl, tm, pos, ub);
  else
    sub_res = mt_last(2 * tv + 1, tm + 1, tr, pos, ub);
  return max(res, sub_res);
}
int uendtime[MAXN];
int usize[MAXN];
vector<int> ug[MAXN];
vector<int> ua[MAXN];
vector<int> uatime[MAXN];
vector<int> uq[MAXN];
long long qres[MAXN];
void u_dfs(int v, int n, vector<int>& atime, vector<long long>& asum) {
  for (int i = int(ua[v].size()) - 1; i >= 0; --i) {
    atime.push_back(uatime[v][i]);
    asum.push_back((asum.empty() ? 0 : asum.back()) + ua[v][i]);
  }
  if (!asum.empty())
    for (int q : uq[v]) {
      auto it1 = lower_bound(atime.begin(), atime.end(), q, greater<int>());
      int pos1 = distance(atime.begin(), it1);
      long long sum1 = asum.back() - (pos1 ? asum[pos1 - 1] : 0);
      long long t2 = mt_last(1, 1, n, mtin[v], q);
      auto it2 = lower_bound(atime.begin(), atime.end(), t2, greater<int>());
      int pos2 = distance(atime.begin(), it2);
      long long sum2 = asum.back() - (pos2 ? asum[pos2 - 1] : 0);
      long long sum = sum1 - sum2;
      qres[q] = sum;
    }
  for (int to : ug[v]) {
    while (!atime.empty() && atime.back() < uendtime[to]) {
      atime.pop_back();
      asum.pop_back();
    }
    u_dfs(to, n, atime, asum);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<query> qs(m + 1);
  for (int i = 1; i <= m; ++i) {
    scanf("%s", s);
    qs[i].type = *s;
    if (qs[i].type == 'U' || qs[i].type == 'M')
      scanf("%d%d", &qs[i].a, &qs[i].b);
    else
      scanf("%d", &qs[i].a);
  }
  for (int i = 1; i <= n; ++i) {
    mg[i].push_back(i);
    mendtime[i] = m + 1;
  }
  for (int i = 1; i <= m; ++i)
    if (qs[i].type == 'M') {
      mg[qs[i].a].push_back(qs[i].b);
      mendtime[qs[i].b] = i;
    }
  int mtimer = 0;
  for (int i = 1; i <= n; ++i)
    if (mendtime[i] == m + 1) mdfs(i, -1, mtimer);
  for (int i = 1; i <= n; ++i) mlpos[i] = mrpos[i] = mtin[i];
  for (int i = 1; i <= m; ++i) {
    int a = qs[i].a;
    int b = qs[i].b;
    if (qs[i].type == 'M') {
      mlpos[a] = min(mlpos[a], mlpos[b]);
      mrpos[a] = max(mrpos[a], mrpos[b]);
    } else if (qs[i].type == 'Z') {
      mt_add(1, 1, n, mlpos[a], mrpos[a], i);
    }
  }
  for (int i = 1; i <= n; ++i) usize[i] = 1;
  for (int i = 1; i <= m; ++i) {
    int a = qs[i].a;
    int b = qs[i].b;
    if (qs[i].type == 'U') {
      uendtime[b] = i;
      usize[a] += usize[b];
      ug[a].push_back(b);
    } else if (qs[i].type == 'A') {
      ua[a].push_back(usize[a]);
      uatime[a].push_back(i);
    } else if (qs[i].type == 'Q') {
      uq[a].push_back(i);
    }
  }
  vector<int> atime;
  vector<long long> asum;
  for (int i = 1; i <= n; ++i)
    if (uendtime[i] == 0) {
      atime.clear();
      asum.clear();
      u_dfs(i, n, atime, asum);
    }
  for (int i = 1; i <= m; ++i)
    if (qs[i].type == 'Q') printf("%lld\n", qres[i]);
  return 0;
}