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;
template <class T>
inline void rd(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
  x *= f;
}
template <class T>
inline void cmax(T &x, T y) {
  if (x < y) x = y;
}
template <class T>
inline void cmin(T &x, T y) {
  if (y < x) x = y;
}
const int N = 5e5 + 10, M = N << 1;
int n, m;
struct Tree {
  int F[M], fa[M], ch[M][2], ti[M], sz[M], ncnt;
  int dfn[M], lb[M], rb[M], id;
  void init() {
    ncnt = n;
    for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;
  }
  int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
  void un(int x, int y, int t) {
    int fx = find(x), fy = find(y), u = ++ncnt;
    fa[u] = fa[fx] = fa[fy] = u, ti[u] = t, sz[u] = sz[fx] + sz[fy];
    ch[u][0] = fx, ch[u][1] = fy;
  }
  void dfs(int u) {
    if (!ch[u][0]) {
      lb[u] = rb[u] = dfn[u] = ++id;
    } else {
      lb[u] = id + 1;
      dfs(ch[u][0]), dfs(ch[u][1]);
      F[ch[u][0]] = F[ch[u][1]] = u;
      rb[u] = id;
    }
  }
  void build() {
    for (int i = 1; i <= ncnt; ++i)
      if (find(i) == i) dfs(i);
    for (int i = 1; i <= n; ++i) fa[i] = i;
  }
  int get(int x, int t) {
    while (F[fa[x]] && ti[F[fa[x]]] <= t) fa[x] = F[fa[x]];
    return fa[x];
  }
} A, B;
namespace seg {
int cov[N << 2];
inline void push_down(int c) {
  if (cov[c]) {
    cmax(cov[c << 1], cov[c]);
    cmax(cov[c << 1 | 1], cov[c]);
    cov[c] = 0;
  }
}
void upd(int l, int r, int c, int L, int R, int v) {
  if (L <= l && R >= r) return (void)(cov[c] = v);
  int mid = l + r >> 1;
  push_down(c);
  if (L <= mid) upd(l, mid, c << 1, L, R, v);
  if (R > mid) upd(mid + 1, r, c << 1 | 1, L, R, v);
}
int query(int l, int r, int c, int v) {
  if (l == r) return cov[c];
  int mid = l + r >> 1;
  push_down(c);
  if (v <= mid)
    return query(l, mid, c << 1, v);
  else
    return query(mid + 1, r, c << 1 | 1, v);
}
}  // namespace seg
namespace Bit {
long long c[N];
void add(int i, int t) {
  for (; i <= n; i += i & -i) c[i] += t;
}
void add(int l, int r, int t) { add(l, t), add(r + 1, -t); }
long long query(int i) {
  long long ans = 0;
  for (; i; i -= i & -i) ans += c[i];
  return ans;
}
}  // namespace Bit
struct OPT {
  int ty, t, x;
  OPT(int ty = 0, int t = 0, int x = 0) : ty(ty), t(t), x(x) {}
};
vector<OPT> Q;
long long ans[N];
struct Que {
  int p, id, sgn;
  Que(int p = 0, int id = 0, int sgn = 0) : p(p), id(id), sgn(sgn) {}
};
vector<Que> R[N];
char ty[20];
int main() {
  rd(n), rd(m);
  A.init(), B.init();
  for (int i = 1, x, y; i <= m; ++i) {
    scanf("%s", ty);
    if (ty[0] == 'U') {
      rd(x), rd(y);
      A.un(x, y, i);
    } else if (ty[0] == 'M') {
      rd(x), rd(y);
      B.un(x, y, i);
    } else {
      rd(x);
      Q.push_back(OPT(ty[0] == 'A' ? 1 : (ty[0] == 'Z' ? 2 : 3), i, x));
    }
  }
  A.build(), B.build();
  int qtot = 0;
  for (int i = 0; i < Q.size(); ++i)
    if (Q[i].ty == 2) {
      int x = B.get(Q[i].x, Q[i].t);
      seg::upd(1, n, 1, B.lb[x], B.rb[x], Q[i].t);
    } else if (Q[i].ty == 3) {
      R[Q[i].t].push_back(Que(A.dfn[Q[i].x], ++qtot, 1));
      int pre = seg::query(1, n, 1, B.dfn[Q[i].x]);
      if (pre) R[pre].push_back(Que(A.dfn[Q[i].x], qtot, -1));
    }
  int q = 0;
  for (int i = 1; i <= m; ++i) {
    while (q < Q.size() && Q[q].t <= i) {
      if (Q[q].ty == 1) {
        int anc = A.get(Q[q].x, Q[q].t);
        Bit::add(A.lb[anc], A.rb[anc], A.sz[anc]);
      }
      q++;
    }
    for (int j = 0; j < R[i].size(); ++j)
      ans[R[i][j].id] += R[i][j].sgn * Bit::query(R[i][j].p);
  }
  for (int i = 1; i <= qtot; ++i) printf("%lld\n", ans[i]);
  return 0;
}