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;
int inf;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T, class T2>
int chkmin(T &a, T2 b) {
  return a > b ? a = b, 1 : 0;
}
template <class T, class T2>
int chkmax(T &a, T2 b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T, class T2>
T mmin(T a, T2 b) {
  return a < b ? a : b;
}
template <class T, class T2>
T mmax(T a, T2 b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
int dcmp(T a, T b) {
  return a > b;
}
template <int *a>
int cmp_a(int first, int second) {
  return a[first] < a[second];
}
template <class T>
bool sort2(T &a, T &b) {
  return a > b ? swap(a, b), 1 : 0;
}
struct __INIT__ {
  __INIT__() {
    fill((unsigned char *)&inf, (unsigned char *)&inf + sizeof(inf), 0x3f);
  }
} __INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char first) {
  *oS++ = first;
  if (oS == oT) flush();
}
template <typename A>
inline bool read(A &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(char &first) {
  while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                               (iS == iT ? EOF : *iS++))
                            : *iS++)) == ' ' ||
         first == '\n' || first == '\r')
    ;
  return first != EOF;
}
inline bool read(char *first) {
  while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                                (iS == iT ? EOF : *iS++))
                             : *iS++)) == '\n' ||
         *first == ' ' || *first == '\r')
    ;
  if (*first == EOF) return 0;
  while (!(*first == '\n' || *first == ' ' || *first == '\r' || *first == EOF))
    *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                           : *iS++);
  *first = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
  return read(first) && read(second...);
}
template <typename A>
inline bool write(A first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char first) {
  putc(first);
  return 0;
}
inline bool write(const char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
inline bool write(char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
  return write(first) || write(second...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
int n, q;
struct Tree {
  int t;
  int fa[1000005];
  vector<int> to[1000005];
  int dfn[1000005], low[1000005], c;
  int find(int first) {
    return first == fa[first] ? first : fa[first] = find(fa[first]);
  }
  void merge(int first, int second) {
    ++t;
    first = find(first), second = find(second);
    fa[first] = fa[second] = fa[t] = t;
    to[t].emplace_back(first);
    to[t].emplace_back(second);
  }
  void dfs(int first) {
    dfn[first] = c + 1;
    if (first <= n) ++c;
    for (auto i : to[first]) dfs(i);
    low[first] = c;
  }
  void make() {
    for (int i = 1; i <= t; ++i)
      if (fa[i] == i) {
        fa[i] = t + 1;
        to[t + 1].emplace_back(i);
      }
    ++t;
    dfs(t);
  }
} t1, t2;
int id[500005];
struct query {
  int tp, first, t;
};
query ask[500005];
long long ans[500005];
set<pair<pair<int, int>, int> > st;
vector<pair<int, int> > q1[500005], q2[500005];
namespace bit {
long long s[500005];
void add(int first, int second) {
  for (; first <= n; first += first & -first) s[first] += second;
}
long long sum(int first) {
  long long second = 0;
  for (; first; first ^= first & -first) second += s[first];
  return second;
}
}  // namespace bit
void split(int first) {
  auto it = st.lower_bound({{first, 0}, 0});
  if (it->first.second < first) {
    int l = it->first.second, r = it->first.first, v = it->second;
    st.erase(it);
    st.insert({{r, first}, v});
    st.insert({{first - 1, l}, v});
  }
}
void put(int l, int r, int first) {
  split(l);
  if (r != n) split(r + 1);
  auto it = st.lower_bound({{l, 0}, 0});
  while (it != st.end() && it->first.first <= r) {
    auto od = it;
    ++it;
    st.erase(od);
  }
  st.insert({{r, l}, first});
}
int getval(int first) { return st.lower_bound({{first, 0}, 0})->second; }
signed main() {
  read(n, q);
  t1.t = t2.t = n;
  for (int i = 1; i <= n; ++i) {
    t1.fa[i] = t2.fa[i] = i;
  }
  int m1 = 0, m = 0;
  for (; q; --q) {
    char tp;
    int first, second;
    read(tp, first);
    switch (tp) {
      case 'U':
        read(second);
        t1.merge(first, second);
        break;
      case 'M':
        read(second);
        t2.merge(first, second);
        break;
      case 'A':
        id[++m1] = t1.find(first);
        break;
      case 'Z':
        ask[++m] = {1, t2.find(first), m1};
        break;
      case 'Q':
        ask[++m] = {2, first, m1};
        break;
    }
  }
  t1.make();
  t2.make();
  st.insert({{n, 1}, 0});
  for (int i = 1; i <= m; ++i) {
    if (ask[i].tp == 1) {
      put(t2.dfn[ask[i].first], t2.low[ask[i].first], ask[i].t);
    } else {
      int xt = getval(t2.dfn[ask[i].first]);
      q1[ask[i].t].emplace_back(t1.dfn[ask[i].first], i);
      q2[xt].emplace_back(t1.dfn[ask[i].first], i);
    }
  }
  for (int i = 1; i <= m1; ++i) {
    int l = t1.dfn[id[i]], r = t1.low[id[i]];
    bit::add(l, r - l + 1);
    bit::add(r + 1, l - r - 1);
    for (auto first : q1[i]) ans[first.second] += bit::sum(first.first);
    for (auto first : q2[i]) ans[first.second] -= bit::sum(first.first);
  }
  for (int i = 1; i <= m; ++i)
    if (ask[i].tp == 2) write(ans[i], '\n');
  return 0;
}