#include <bits/stdc++.h>
using namespace std;
const int N = 5 * 1e5 + 100;
int n, m, ans[N], w;
struct ask {
  int op, x, y, wh;
} a[N];
struct pr {
  int a, b;
};
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
struct tree {
  int dfn[N * 2], cnt, out[N * 2], son[N * 2][2], up[N], id[N * 2];
  int kind, vi[N * 2], sz[N * 2];
  void dfs(int x) {
    if (x <= n) {
      out[x] = dfn[x] = ++cnt;
      sz[x] = 1;
      return;
    }
    if (son[x][0]) dfs(son[x][0]);
    dfn[x] = dfn[son[x][0]];
    if (son[x][1]) dfs(son[x][1]);
    sz[x] = sz[son[x][0]] + sz[son[x][1]];
    out[x] = cnt;
  }
  inline void init() {
    for (int i = 1; i <= n; i++) up[i] = i, vi[i] = 1;
    int w = n;
    for (int i = 1; i <= m; i++) {
      if (a[i].op == kind) {
        int x = up[a[i].x], y = up[a[i].y];
        son[++w][0] = x;
        son[w][1] = y;
        vi[x] = vi[y] = 0;
        up[a[i].x] = w;
        vi[w] = 1;
      }
      if (a[i].op == kind + 2) a[i].wh = up[a[i].x];
    }
    for (int i = 1; i <= w; i++)
      if (vi[i]) dfs(i);
  }
} A;
tree B;
namespace seg {
int ti[N * 4], val[N * 4];
inline void init() { memset(ti, -1, sizeof(ti)); }
void change(int x, int l, int r, int ll, int rr, int when, int v) {
  if (ll <= l && rr >= r) {
    if (when > ti[x]) ti[x] = when, val[x] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (ll <= mid) change(x + x, l, mid, ll, rr, when, v);
  if (rr > mid) change(x + x + 1, mid + 1, r, ll, rr, when, v);
}
pr query(int x, int l, int r, int wh) {
  if (l == r) return (pr){ti[x], val[x]};
  int mid = (l + r) >> 1;
  pr ans;
  if (wh <= mid)
    ans = query(x + x, l, mid, wh);
  else
    ans = query(x + x + 1, mid + 1, r, wh);
  if (ti[x] > ans.a) ans = (pr){ti[x], val[x]};
  return ans;
}
}  // namespace seg
namespace ment {
vector<int> ti[N * 4];
vector<long long> sum[N * 4];
void change(int x, int l, int r, int ll, int rr, int t, int v) {
  if (ll <= l && rr >= r) {
    ti[x].push_back(t);
    if (!sum[x].empty())
      sum[x].push_back(sum[x].back() + v);
    else
      sum[x].push_back(v);
    return;
  }
  int mid = (l + r) >> 1;
  if (ll <= mid) change(x + x, l, mid, ll, rr, t, v);
  if (rr > mid) change(x + x + 1, mid + 1, r, ll, rr, t, v);
}
inline long long cal(int x, int tl, int tr) {
  if (sum[x].empty()) return 0ll;
  if (tl <= ti[x][0]) return sum[x].back();
  int L = upper_bound(ti[x].begin(), ti[x].end(), tl - 1) - ti[x].begin() - 1;
  return sum[x].back() - sum[x][L];
}
long long query(int x, int l, int r, int wh, int tl, int tr) {
  if (l == r) return cal(x, tl, tr);
  int mid = (l + r) >> 1;
  long long ans = 0;
  if (wh <= mid)
    ans = query(x + x, l, mid, wh, tl, tr);
  else
    ans = query(x + x + 1, mid + 1, r, wh, tl, tr);
  ans += cal(x, tl, tr);
  return ans;
}
}  // namespace ment
signed main() {
  n = read();
  m = read();
  char s;
  for (int i = 1; i <= m; i++) {
    s = getchar();
    getchar();
    if (s == 'U') a[i].op = 1;
    if (s == 'M') a[i].op = 2;
    if (s == 'A') a[i].op = 3;
    if (s == 'Z') a[i].op = 4;
    if (s == 'Q') a[i].op = 5;
    a[i].x = read();
    if (a[i].op <= 2) a[i].y = read();
  }
  A.kind = 1;
  B.kind = 2;
  A.init();
  B.init();
  seg::init();
  for (int i = 1; i <= m; i++) {
    if (a[i].op <= 2) continue;
    if (a[i].op == 3) {
      a[i].y = A.sz[a[i].wh];
      int l = A.dfn[a[i].wh], r = A.out[a[i].wh];
      ment::change(1, 1, n, l, r, i, a[i].y);
    }
    if (a[i].op == 4) {
      a[i].y = 0;
      int l = B.dfn[a[i].wh], r = B.out[a[i].wh];
      seg::change(1, 1, n, l, r, i, a[i].y);
    }
    if (a[i].op == 5) {
      pr now = seg::query(1, 1, n, B.dfn[a[i].x]);
      long long ans = ment::query(1, 1, n, A.dfn[a[i].x], now.a, i) + now.b;
      printf("%lld\n", ans);
    }
  }
}
