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 N = 5 * 1e5 + 100;
int n, m, ans[N], w;
struct ask {
  int op, x, y, wh;
} a[N];
struct pr {
  int a, b;
};
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
struct tree {
  int dfn[N * 2], cnt, out[N * 2], son[N * 2][2], up[N], id[N * 2];
  int kind, vi[N * 2], sz[N * 2];
  void dfs(int x) {
    if (x <= n) {
      out[x] = dfn[x] = ++cnt;
      sz[x] = 1;
      return;
    }
    if (son[x][0]) dfs(son[x][0]);
    dfn[x] = dfn[son[x][0]];
    if (son[x][1]) dfs(son[x][1]);
    sz[x] = sz[son[x][0]] + sz[son[x][1]];
    out[x] = cnt;
  }
  inline void init() {
    for (int i = 1; i <= n; i++) up[i] = i, vi[i] = 1;
    int w = n;
    for (int i = 1; i <= m; i++) {
      if (a[i].op == kind) {
        int x = up[a[i].x], y = up[a[i].y];
        son[++w][0] = x;
        son[w][1] = y;
        vi[x] = vi[y] = 0;
        up[a[i].x] = w;
        vi[w] = 1;
      }
      if (a[i].op == kind + 2) a[i].wh = up[a[i].x];
    }
    for (int i = 1; i <= w; i++)
      if (vi[i]) dfs(i);
  }
} A;
tree B;
namespace seg {
int ti[N * 4], val[N * 4];
inline void init() { memset(ti, -1, sizeof(ti)); }
void change(int x, int l, int r, int ll, int rr, int when, int v) {
  if (ll <= l && rr >= r) {
    if (when > ti[x]) ti[x] = when, val[x] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (ll <= mid) change(x + x, l, mid, ll, rr, when, v);
  if (rr > mid) change(x + x + 1, mid + 1, r, ll, rr, when, v);
}
pr query(int x, int l, int r, int wh) {
  if (l == r) return (pr){ti[x], val[x]};
  int mid = (l + r) >> 1;
  pr ans;
  if (wh <= mid)
    ans = query(x + x, l, mid, wh);
  else
    ans = query(x + x + 1, mid + 1, r, wh);
  if (ti[x] > ans.a) ans = (pr){ti[x], val[x]};
  return ans;
}
}  // namespace seg
namespace ment {
vector<int> ti[N * 4];
vector<long long> sum[N * 4];
void change(int x, int l, int r, int ll, int rr, int t, int v) {
  if (ll <= l && rr >= r) {
    ti[x].push_back(t);
    if (!sum[x].empty())
      sum[x].push_back(sum[x].back() + v);
    else
      sum[x].push_back(v);
    return;
  }
  int mid = (l + r) >> 1;
  if (ll <= mid) change(x + x, l, mid, ll, rr, t, v);
  if (rr > mid) change(x + x + 1, mid + 1, r, ll, rr, t, v);
}
inline long long cal(int x, int tl, int tr) {
  if (sum[x].empty()) return 0ll;
  if (tl <= ti[x][0]) return sum[x].back();
  int L = upper_bound(ti[x].begin(), ti[x].end(), tl - 1) - ti[x].begin() - 1;
  return sum[x].back() - sum[x][L];
}
long long query(int x, int l, int r, int wh, int tl, int tr) {
  if (l == r) return cal(x, tl, tr);
  int mid = (l + r) >> 1;
  long long ans = 0;
  if (wh <= mid)
    ans = query(x + x, l, mid, wh, tl, tr);
  else
    ans = query(x + x + 1, mid + 1, r, wh, tl, tr);
  ans += cal(x, tl, tr);
  return ans;
}
}  // namespace ment
signed main() {
  n = read();
  m = read();
  char s;
  for (int i = 1; i <= m; i++) {
    s = getchar();
    getchar();
    if (s == 'U') a[i].op = 1;
    if (s == 'M') a[i].op = 2;
    if (s == 'A') a[i].op = 3;
    if (s == 'Z') a[i].op = 4;
    if (s == 'Q') a[i].op = 5;
    a[i].x = read();
    if (a[i].op <= 2) a[i].y = read();
  }
  A.kind = 1;
  B.kind = 2;
  A.init();
  B.init();
  seg::init();
  for (int i = 1; i <= m; i++) {
    if (a[i].op <= 2) continue;
    if (a[i].op == 3) {
      a[i].y = A.sz[a[i].wh];
      int l = A.dfn[a[i].wh], r = A.out[a[i].wh];
      ment::change(1, 1, n, l, r, i, a[i].y);
    }
    if (a[i].op == 4) {
      a[i].y = 0;
      int l = B.dfn[a[i].wh], r = B.out[a[i].wh];
      seg::change(1, 1, n, l, r, i, a[i].y);
    }
    if (a[i].op == 5) {
      pr now = seg::query(1, 1, n, B.dfn[a[i].x]);
      long long ans = ment::query(1, 1, n, A.dfn[a[i].x], now.a, i) + now.b;
      printf("%lld\n", ans);
    }
  }
}