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;
struct query {
  char c;
  int x, y;
  long long ans;
  query *child;
  void read() {
    scanf(" %c %d", &c, &x);
    if (c == 'U' || c == 'M') {
      scanf("%d", &y);
    }
  }
};
int N, M;
query Q[2 * (1 << 19)];
struct virus {
  int ch[2 * (1 << 19)][2];
  int lt[2 * (1 << 19)], rt[2 * (1 << 19)];
  int chpar[2 * (1 << 19)];
  int cnode;
  int curn[(1 << 19)];
  int ord[(1 << 19)], idx[(1 << 19)];
  int ufpar[(1 << 19)];
  void setchild(int cur, int dir, int x) {
    ch[cur][dir] = x;
    chpar[x] = cur;
  }
  int find(int x) { return x == ufpar[x] ? x : ufpar[x] = find(ufpar[x]); }
  void merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      puts("BAD MERGE");
      exit(0);
    }
    ufpar[x] = y;
    curn[y] = chpar[curn[y]];
  }
  void reset() {
    for (int i = 1; i <= N; i++) {
      curn[i] = ufpar[i] = i;
    }
  }
} john, tukey;
void inorder(virus &tr, int x, int &ptr) {
  if (x > N) {
    for (int i = 0; i < 2; i++) {
      inorder(tr, tr.ch[x][i], ptr);
    }
    tr.lt[x] = tr.lt[tr.ch[x][0]];
    tr.rt[x] = tr.rt[tr.ch[x][1]];
  } else {
    ptr++;
    tr.ord[ptr] = x;
    tr.idx[x] = ptr;
  }
}
void filter(char c, virus &tr) {
  tr.reset();
  for (int i = 1; i <= N; i++) {
    tr.lt[i] = tr.rt[i] = i;
  }
  int &ctr = tr.cnode;
  ctr = N;
  for (int i = 1; i <= M; i++) {
    if (Q[i].c != c) {
      continue;
    }
    int x = tr.find(Q[i].x), y = tr.find(Q[i].y);
    ctr++;
    tr.setchild(ctr, 0, tr.curn[x]);
    tr.setchild(ctr, 1, tr.curn[y]);
    tr.merge(x, y);
  }
  int ptr = 0;
  for (int i = 1; i <= ctr; i++) {
    if (tr.chpar[i]) {
      continue;
    }
    inorder(tr, i, ptr);
  }
  if (ptr != N) {
    printf("UHOH %d\n", ptr);
    exit(0);
  }
  tr.reset();
}
int tree[2 * (1 << 19)], lazy[2 * (1 << 19)];
long long bit[(1 << 19)];
vector<query> qbef[(1 << 19)];
void add(int cur, int v) {
  tree[cur] = v;
  lazy[cur] = v;
}
void down(int cur) {
  if (lazy[cur]) {
    add(2 * cur, lazy[cur]);
    add(2 * cur + 1, lazy[cur]);
    lazy[cur] = 0;
  }
}
void update(int a, int b, int v, int cur = 1, int l = 0, int r = (1 << 19)) {
  if (r <= a || b <= l) {
    return;
  }
  if (a <= l && r <= b) {
    add(cur, v);
    return;
  }
  down(cur);
  int mid = (l + r) / 2;
  update(a, b, v, 2 * cur, l, mid);
  update(a, b, v, 2 * cur + 1, mid, r);
}
int qtree(int a, int cur = 1, int l = 0, int r = (1 << 19)) {
  if (r == l + 1) {
    return tree[cur];
  }
  down(cur);
  int mid = (l + r) / 2;
  return a < mid ? qtree(a, 2 * cur, l, mid) : qtree(a, 2 * cur + 1, mid, r);
}
void upbit(int x, int v) {
  for (; x < (1 << 19); x += (x & -x)) {
    bit[x] += v;
  }
}
long long qubit(int x) {
  long long s = 0;
  for (; x; x &= x - 1) {
    s += bit[x];
  }
  return s;
}
void go(bool second) {
  for (int i = 1; i <= M; i++) {
    if (second) {
      for (query qu : qbef[i]) {
        qu.child->ans -= qubit(john.idx[qu.x]);
      }
    }
    char c = Q[i].c;
    int x = Q[i].x, y = Q[i].y;
    if (c == 'M') {
      tukey.merge(x, y);
    } else if (c == 'U') {
      john.merge(x, y);
    } else if (c == 'A') {
      int cn = john.curn[john.find(x)];
      int ltidx = john.idx[john.lt[cn]], rtidx = john.idx[john.rt[cn]];
      int sz = rtidx - ltidx + 1;
      upbit(ltidx, sz);
      upbit(rtidx + 1, -sz);
    } else if (c == 'Z') {
      if (second) {
        continue;
      }
      int cn = tukey.curn[tukey.find(x)];
      int ltidx = tukey.idx[tukey.lt[cn]], rtidx = tukey.idx[tukey.rt[cn]];
      update(ltidx, rtidx + 1, i);
    } else {
      if (second) {
        cout << Q[i].ans << '\n';
      } else {
        Q[i].ans = qubit(john.idx[x]);
        int raid = qtree(tukey.idx[x]);
        query qu;
        qu.x = x;
        qu.child = Q + i;
        qbef[raid].push_back(qu);
      }
    }
  }
}
int main() {
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
  }
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= M; i++) {
    Q[i].read();
  }
  filter('U', john);
  filter('M', tukey);
  go(false);
  tukey.reset();
  john.reset();
  memset((tree), (0), sizeof(tree));
  memset((lazy), (0), sizeof(lazy));
  memset((bit), (0), sizeof(bit));
  go(true);
}