#include <bits/stdc++.h>
const int N = 500005;
using namespace std;
int n, m, al, u, v;
char op[3];
long long va[N], ans;
struct T {
  int f[N], sz[N], ui[N];
  vector<int> t[N];
  inline void push() {
    for (; f[u] ^ u; u = f[u])
      ;
    va[al] = sz[u] + (t[u].size() ? va[t[u][t[u].size() - 1]] : 0);
    t[u].push_back(al);
  }
  inline void uni() {
    for (; f[u] ^ u; u = f[u])
      ;
    for (; f[v] ^ v; v = f[v])
      ;
    if (sz[u] < sz[v]) swap(u, v);
    sz[f[v] = u] += sz[v];
    ui[v] = t[u].size();
  }
  inline void get(int u) {
    int tp = 0;
    for (;; tp = ui[u], u = f[u]) {
      if (t[u].size() > tp) v = t[u][t[u].size() - 1];
      if (f[u] == u) break;
    }
  }
  inline void sol() {
    int tp = 0, re;
    for (;; tp = ui[u], u = f[u]) {
      if (t[u].size() > tp && t[u][t[u].size() - 1] > v) {
        ans += va[t[u][t[u].size() - 1]];
        if (t[u][tp] < v) {
          re = tp - 1;
          for (int l = tp, r = t[u].size() - 1, mi; l <= r;)
            if (t[u][mi = l + r >> 1] < v)
              re = mi, l = mi + 1;
            else
              r = mi - 1;
          ans -= va[t[u][re]];
        } else if (tp)
          ans -= va[t[u][tp - 1]];
      }
      if (f[u] == u) break;
    }
  }
} A, D;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) A.f[i] = D.f[i] = i, A.sz[i] = D.sz[i] = 1;
  for (al = 1; al <= m; al++) {
    scanf("%s%d", op, &u);
    if (op[0] == 'A')
      A.push();
    else if (op[0] == 'Z')
      D.push();
    else if (op[0] == 'Q') {
      v = ans = 0;
      D.get(u);
      A.sol();
      printf("%lld\n", ans);
    } else
      scanf("%d", &v), op[0] ^ 'U' ? D.uni() : A.uni();
  }
}
