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 = 1e6 + 7;
struct SegmentTree {
  int w[MAXN << 2], l[MAXN << 2], r[MAXN << 2];
  void build(int L, int R, int rt = 1) {
    l[rt] = L;
    r[rt] = R;
    w[rt] = 0;
    if (L + 1 == R) return;
    int mid = (L + R) >> 1;
    build(L, mid, rt << 1);
    build(mid, R, rt << 1 | 1);
  }
  void pushdown(int rt) {
    if (w[rt]) w[rt << 1] = w[rt << 1 | 1] = w[rt], w[rt] = 0;
  }
  void modify(int L, int R, int x, int rt = 1) {
    if (L >= r[rt] or l[rt] >= R) return;
    if (L <= l[rt] and r[rt] <= R) return void(w[rt] = x);
    pushdown(x);
    modify(L, R, x, rt << 1);
    modify(L, R, x, rt << 1 | 1);
  }
  int query(int pos, int rt = 1) {
    if (l[rt] + 1 == r[rt]) return w[rt];
    pushdown(rt);
    int mid = (l[rt] + r[rt]) >> 1;
    if (pos < mid)
      return query(pos, rt << 1);
    else
      return query(pos, rt << 1 | 1);
  }
} seg;
struct FenwickTree {
  long long sum[MAXN];
  void modify(int pos, int x) {
    while (pos < MAXN) sum[pos] += x, pos += (pos & (-pos));
  }
  long long qsum(int pos) {
    long long ret = 0;
    while (pos) ret += sum[pos], pos -= (pos & (-pos));
    return ret;
  }
} bit;
int n, q;
long long ret[MAXN];
struct Disjoint_Set_Union {
  int fa[MAXN];
  int findx(int x) { return x == fa[x] ? x : fa[x] = findx(fa[x]); }
  void merge(int x, int y) {
    if (findx(x) != findx(y)) fa[findx(x)] = findx(y);
  }
} dsu[2];
struct Graph {
  vector<int> g[MAXN];
  int tot, st[MAXN], sz[MAXN], idx, ed[MAXN];
  vector<int> &operator[](int ID) { return g[ID]; }
  void dfs(int u) {
    sz[u] = u <= n;
    st[u] = ++idx;
    for (int v : g[u]) dfs(v), sz[u] += sz[v];
    ed[u] = idx;
  }
} G[2];
struct Event {
  int op, x, y, time, qid;
} Q[MAXN];
void init() {
  for (int i = 1; i <= (n << 1); i++) {
    dsu[0].fa[i] = dsu[1].fa[i] = i;
    G[0][i].clear();
    G[1][i].clear();
    bit.sum[i] = 0;
  }
  G[0].tot = G[1].tot = n;
  G[0].idx = G[1].idx = 0;
  for (int i = 1; i <= q; i++) ret[i] = 0;
}
int exc(char ch) {
  if (ch == 'U') return 1;
  if (ch == 'M') return 2;
  if (ch == 'A') return 3;
  if (ch == 'Z') return 4;
  if (ch == 'Q') return 5;
}
char buf[233];
void solve() {
  scanf("%d %d", &n, &q);
  init();
  for (int i = 1; i <= q; i++) {
    scanf("%s", buf);
    Q[i].op = exc(buf[0]);
    assert(Q[i].op >= 1 and Q[i].op <= 5);
    Q[i].time = i;
    if (Q[i].op == 1) {
      scanf("%d %d", &Q[i].x, &Q[i].y);
      if (dsu[0].findx(Q[i].x) == dsu[0].findx(Q[i].y)) continue;
      G[0].tot++;
      G[0][G[0].tot].push_back(dsu[0].findx(Q[i].x));
      G[0][G[0].tot].push_back(dsu[0].findx(Q[i].y));
      dsu[0].merge(Q[i].x, G[0].tot);
      dsu[0].merge(Q[i].y, G[0].tot);
    } else if (Q[i].op == 2) {
      scanf("%d %d", &Q[i].x, &Q[i].y);
      if (dsu[1].findx(Q[i].x) == dsu[1].findx(Q[i].y)) continue;
      G[1].tot++;
      G[1][G[1].tot].push_back(dsu[1].findx(Q[i].x));
      G[1][G[1].tot].push_back(dsu[1].findx(Q[i].y));
      dsu[1].merge(Q[i].x, G[1].tot);
      dsu[1].merge(Q[i].y, G[1].tot);
    } else if (Q[i].op == 3) {
      scanf("%d %d", &Q[i].x);
      Q[i].x = dsu[0].findx(Q[i].x);
    } else if (Q[i].op == 4) {
      scanf("%d", &Q[i].x);
      Q[i].x = dsu[1].findx(Q[i].x);
    } else
      scanf("%d", &Q[i].x);
  }
  G[0].tot++;
  G[1].tot++;
  for (int i = 1; i < G[0].tot; i++)
    if (dsu[0].findx(i) == i) G[0][G[0].tot].push_back(i);
  for (int i = 1; i < G[1].tot; i++)
    if (dsu[1].findx(i) == i) G[1][G[1].tot].push_back(i);
  G[0].dfs(G[0].tot);
  G[1].dfs(G[1].tot);
  seg.build(1, MAXN);
  assert(G[0].tot <= (n << 1) and G[1].tot <= (n << 1));
  vector<Event> vec;
  int cnt_q = 0;
  for (int i = 1; i <= q; i++) {
    if (Q[i].op == 3)
      vec.push_back(Q[i]);
    else if (Q[i].op == 4)
      seg.modify(G[1].st[Q[i].x], G[1].ed[Q[i].x] + 1, Q[i].time);
    else if (Q[i].op == 5) {
      Q[i].qid = ++cnt_q;
      Q[i].y = 1;
      vec.push_back(Q[i]);
      Q[i].y = -1;
      Q[i].time = seg.query(G[1].st[Q[i].x]);
      if (Q[i].time) vec.push_back(Q[i]);
    }
  }
  sort(vec.begin(), vec.end(),
       [&](const Event &A, const Event &B) { return A.time < B.time; });
  for (auto &qs : vec) {
    if (qs.op == 3) {
      bit.modify(G[0].st[qs.x], G[0].sz[qs.x]);
      bit.modify(G[0].ed[qs.x] + 1, -G[0].sz[qs.x]);
    } else
      ret[qs.qid] += qs.y * bit.qsum(G[0].st[qs.x]);
  }
  for (int i = 1; i <= cnt_q; i++) printf("%lld\n", ret[i]);
}
int main() {
  solve();
  return 0;
}