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 __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;
template <typename T>
inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = (iS == iT
                            ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48),
    ch = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),
                      (iS == iT ? EOF : *iS++))
                   : *iS++);
  x = t ? -x : x;
}
inline char read_alpha() {
  char c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),
                        (iS == iT ? EOF : *iS++))
                     : *iS++);
  while (!isalpha(c) && c != EOF)
    c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),
                     (iS == iT ? EOF : *iS++))
                  : *iS++);
  return c;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline long long read_ll() {
  long long x;
  return read(x), x;
}
template <typename T>
inline void chkmin(T &a, T b) {
  a = a < b ? a : b;
}
template <typename T>
inline void chkmax(T &a, T b) {
  a = a > b ? a : b;
}
const int MAXN = 1000010;
int n, q;
struct Bit {
  int N;
  long long T[MAXN];
  inline void Add(int p, long long v) {
    while (p <= N) T[p] += v, p += p & -p;
  }
  inline void Modify(int l, int r, int v) { Add(l, v), Add(r + 1, -v); }
  inline long long Q(int p) {
    long long v = 0;
    while (p) v += T[p], p ^= p & -p;
    return v;
  }
} bit;
struct Segment_Tree {
  int T[MAXN << 2];
  inline void Modify(int x, int l, int r, int L, int R, int v) {
    if (L <= l && r <= R) return T[x] = v, void();
    int mid = (l + r) >> 1;
    if (L <= mid) Modify((x << 1), l, mid, L, R, v);
    if (mid < R) Modify((x << 1 | 1), mid + 1, r, L, R, v);
  }
  inline int Q(int x, int l, int r, int p) {
    if (T[x] || l == r) return T[x];
    int mid = (l + r) >> 1;
    return p <= mid ? Q((x << 1), l, mid, p) : Q((x << 1 | 1), mid + 1, r, p);
  }
} seg;
struct Tree {
  int N;
  struct UFS {
    int fa[MAXN];
    int sz[MAXN];
    inline void init() {
      for (int i = 1; i <= n + q; i++) fa[i] = i, sz[i] = i <= n;
    }
    inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
    inline int size(int x) { return sz[find(x)]; }
    inline void Merge(int x, int y) {
      x = find(x), y = find(y);
      if (x == y) return;
      fa[y] = x, sz[x] += sz[y];
    }
  } ufs;
  int L[MAXN];
  int R[MAXN];
  inline int Qfa(int x) { return ufs.find(x); }
  inline int Qsize(int x) { return ufs.size(x); }
  inline void Merge(int x, int y) {
    ++N;
    L[N] = ufs.find(x);
    R[N] = ufs.find(y);
    ufs.Merge(N, x);
    ufs.Merge(N, y);
  }
  int dfn[MAXN];
  int las[MAXN];
  int tim;
  inline void dfs(int x) {
    dfn[x] = ++tim;
    if (L[x]) dfs(L[x]);
    if (R[x]) dfs(R[x]);
    las[x] = tim;
  }
  inline void Prework() {
    for (int i = 1; i <= N; i++)
      if (ufs.find(i) == i) dfs(i);
  }
} A, B;
struct Opeartor {
  int op, x, y;
  inline void rd() {
    char c = read_alpha();
    switch (c) {
      case 'U':
        op = 1;
        x = read_int();
        y = read_int();
        A.Merge(x, y);
        break;
      case 'M':
        op = 2;
        x = read_int();
        y = read_int();
        B.Merge(x, y);
        break;
      case 'A':
        op = 3;
        x = A.Qfa(read_int());
        y = A.Qsize(x);
        break;
      case 'Z':
        op = 4;
        x = B.Qfa(read_int());
        break;
      case 'Q':
        op = 5;
        x = read_int();
        break;
    }
  }
} op[MAXN];
long long res[MAXN];
struct Query {
  int t, p, id;
  Query() {}
  Query(int t, int p, int id) : t(t), p(p), id(id) {}
  inline int sgn() { return t == id ? 1 : -1; }
  friend bool operator<(Query a, Query b) { return a.t < b.t; }
} Q[MAXN];
int main() {
  n = read_int(), q = read_int();
  A.N = B.N = n;
  A.ufs.init(), B.ufs.init();
  for (int i = 1; i <= q; i++) op[i].rd();
  A.Prework(), B.Prework(), bit.N = A.N;
  int qry = 0, now = 0;
  for (int i = 1; i <= q; i++) {
    if (op[i].op == 4) seg.Modify(1, 1, B.N, B.dfn[op[i].x], B.las[op[i].x], i);
    if (op[i].op == 5) {
      int x = op[i].x;
      Q[++qry] = Query(i, x, i);
      Q[++qry] = Query(seg.Q(1, 1, B.N, B.dfn[x]), x, i);
    }
  }
  sort(Q + 1, Q + 1 + qry);
  while (!Q[now].t) ++now;
  for (int i = 1; i <= q; i++) {
    int x = op[i].x, y = op[i].y;
    if (op[i].op == 3) bit.Modify(A.dfn[x], A.las[x], y);
    while (Q[now].t == i) {
      res[Q[now].id] += Q[now].sgn() * bit.Q(A.dfn[Q[now].p]);
      ++now;
    }
  }
  for (int i = 1; i <= q; i++)
    if (op[i].op == 5) printf("%lld\n", res[i]);
  return 0;
}