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>
struct edge {
  int to, nxt;
} e1[1000005], e2[1000005];
int h1[1000005], h2[1000005], cnt1, cnt2;
int c[1000005];
struct que {
  short ty;
  int id, x, tid;
} q[1000005];
long long ans[1000005], T[1000005 << 2];
int dfn[1000005], tot, siz[1000005];
int n, m, N;
int N1, N2;
int NN[1000005], mx[1000005], my[1000005];
int fa1[1000005], fa2[1000005], sz[1000005];
int go2[1000005];
bool vis[1000005];
char s[5];
short ty = 1;
int find1(int x) { return fa1[x] = fa1[x] == x ? x : find1(fa1[x]); }
int find2(int x) { return fa2[x] = ((fa2[x] == x) ? x : find2(fa2[x])); }
void ins1(int x, int y) {
  e1[++cnt1].nxt = h1[x];
  h1[x] = cnt1;
  e1[cnt1].to = y;
}
void ins2(int x, int y) {
  e2[++cnt2].nxt = h2[x];
  h2[x] = cnt2;
  e2[cnt2].to = y;
}
void dfs1(int x) {
  dfn[x] = ++tot;
  siz[x] = 1;
  for (int i = h1[x]; i; i = e1[i].nxt) dfs1(e1[i].to), siz[x] += siz[e1[i].to];
}
void dfs2(int x) {
  dfn[x] = ++tot;
  siz[x] = 1;
  for (int i = h2[x]; i; i = e2[i].nxt) dfs2(e2[i].to), siz[x] += siz[e2[i].to];
}
void push_down(int x) {
  if (T[x]) {
    if (ty) {
      T[x << 1] = T[x];
      T[x << 1 | 1] = T[x];
    } else {
      T[x << 1] += T[x];
      T[x << 1 | 1] += T[x];
    }
    T[x] = 0;
  }
}
void modify(int x, int l, int r, int L, int R, int v) {
  if (r < L || l > R) return;
  if (l >= L && r <= R) return (void)(ty ? (T[x] = v) : (T[x] += v));
  push_down(x);
  int mid = (l + r) >> 1;
  modify(x << 1, l, mid, L, R, v);
  modify(x << 1 | 1, mid + 1, r, L, R, v);
}
long long query(int x, int l, int r, int pos) {
  if (l == r) return T[x];
  int mid = (l + r) >> 1;
  push_down(x);
  if (pos <= mid) return query(x << 1, l, mid, pos);
  return query(x << 1 | 1, mid + 1, r, pos);
}
bool cmp(que x, que y) { return x.tid < y.tid; }
int main() {
  scanf("%d%d", &n, &m);
  N1 = N2 = n;
  for (int i = 1; i <= (n << 1); i++) fa1[i] = fa2[i] = i, sz[i] = (i <= n);
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%s%d", s, &x);
    if (s[0] == 'U') {
      scanf("%d", &y);
      ++N1;
      mx[i] = x;
      my[i] = y;
      NN[i] = N1;
      x = find1(x);
      y = find1(y);
      sz[N1] += sz[x];
      sz[N1] += sz[y];
      ins1(N1, x);
      ins1(N1, y);
      fa1[x] = N1;
      fa1[y] = N1;
    } else if (s[0] == 'M') {
      scanf("%d", &y);
      ++N2;
      mx[i] = x;
      my[i] = y;
      NN[i] = -N2;
      x = find2(x);
      y = find2(y);
      ins2(N2, x);
      ins2(N2, y);
      fa2[x] = N2;
      fa2[y] = N2;
    } else if (s[0] == 'A') {
      c[i] = x;
    } else if (s[0] == 'Z') {
      c[i] = -x;
    } else {
      ++N;
      q[N].id = N;
      q[N].tid = i;
      q[N].x = x;
      q[N].ty = 1;
    }
  }
  ++N1;
  for (int i = 1; i <= n; i++)
    if (!vis[find1(i)]) {
      vis[find1(i)] = 1;
      ins1(N1, find1(i));
    }
  ++N2;
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; i++)
    if (!vis[find2(i)]) {
      vis[find2(i)] = 1;
      ins2(N2, find2(i));
    }
  dfs2(N2);
  for (int i = 1; i <= std::max(N1, N2); i++) fa1[i] = fa2[i] = i;
  for (int i = 1, j = 1; i <= N; i++) {
    for (; j <= q[i].tid; j++) {
      if (c[j] < 0)
        modify(1, 1, N2, dfn[find2(-c[j])],
               dfn[find2(-c[j])] + siz[find2(-c[j])] - 1, j);
      if (NN[j] < 0) fa2[find2(mx[j])] = -NN[j], fa2[find2(my[j])] = -NN[j];
    }
    q[i + N].id = i;
    q[i + N].tid = query(1, 1, N2, dfn[q[i].x]);
    q[i + N].ty = -1;
    q[i + N].x = q[i].x;
  }
  N <<= 1;
  tot = 0;
  dfs1(N1);
  memset(T, 0, sizeof T);
  ty = 0;
  std::sort(q + 1, q + 1 + N, cmp);
  for (int i = 1, j = 1; i <= N; i++) {
    for (; j <= q[i].tid; j++) {
      if (c[j] > 0)
        modify(1, 1, N1, dfn[find1(c[j])],
               dfn[find1(c[j])] + siz[find1(c[j])] - 1, sz[find1(c[j])]);
      if (NN[j] > 0) fa1[find1(mx[j])] = NN[j], fa1[find1(my[j])] = NN[j];
    }
    ans[q[i].id] += q[i].ty * query(1, 1, N1, dfn[q[i].x]);
  }
  N >>= 1;
  for (int i = 1; i <= N; i++) printf("%lld\n", ans[i]);
}