#include <bits/stdc++.h>
using namespace std;
int n, m, root[1000010], fa[1000010], maxv[1000010], sz[1000010], va[1000010];
int op[1000010][2], tot;
long long sum[1000010], ans[1000010];
char kind[1000010];
vector<int> query[1000010];
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
int getmax(int x) {
  if (fa[x] == x) return maxv[x];
  maxv[x] = max(maxv[x], getmax(fa[x]));
  return maxv[x];
}
long long getsum(int x) {
  if (fa[x] == x) return 0;
  sum[x] = getsum(fa[x]) + sum[x];
  return sum[x];
}
long long calc(int x) {
  getsum(x);
  getfa(x);
  if (fa[x] == x)
    return sum[x];
  else
    return sum[x] + sum[fa[x]];
}
int main() {
  scanf("%d%d", &n, &m);
  char comm[10];
  tot = n;
  memset(maxv, 0, sizeof maxv);
  for (int i = 1; i <= n; i++) root[i] = i, fa[i] = i, maxv[i] = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%s", comm);
    kind[i] = comm[0];
    scanf("%d", &op[i][0]);
    if (kind[i] == 'U' || kind[i] == 'M') scanf("%d", &op[i][1]);
    if (kind[i] == 'Q') {
      query[getmax(op[i][0])].push_back(-i);
      getfa(op[i][0]);
      query[i].push_back(i);
    }
    if (kind[i] == 'M') {
      ++tot;
      maxv[tot] = 0;
      fa[tot] = tot;
      fa[root[op[i][0]]] = tot;
      fa[root[op[i][1]]] = tot;
      root[op[i][0]] = tot;
      root[op[i][1]] = 0;
    }
    if (kind[i] == 'Z') {
      maxv[root[op[i][0]]] = i;
    }
  }
  memset(sum, 0, sizeof sum);
  memset(va, 0, sizeof va);
  for (int i = 1; i <= n; i++)
    root[i] = fa[i] = i, sum[i] = va[i] = 0, sz[i] = 1;
  tot = n;
  for (int i = 1; i <= m; i++) ans[i] = 0;
  for (int i = 1; i <= m; i++) {
    int x = op[i][0], y = op[i][1];
    if (kind[i] == 'U') {
      ++tot;
      sz[tot] = sz[root[x]] + sz[root[y]];
      fa[root[x]] = fa[root[y]] = tot;
      root[x] = tot;
      sum[tot] = va[tot] = 0;
      fa[tot] = tot;
    }
    if (kind[i] == 'A') {
      sum[root[x]] += sz[root[x]];
    }
    for (int j = 0; j < (int)query[i].size(); j++) {
      int t = abs(query[i][j]);
      long long tmp = calc(op[t][0]);
      if (query[i][j] < 0)
        ans[t] -= tmp;
      else
        ans[t] += tmp;
    }
  }
  for (int i = 1; i <= m; i++)
    if (kind[i] == 'Q') printf("%lld\n", ans[i]);
  return 0;
}
