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;
char tp[1048576];
int x[1048576], y[1048576];
int n, m;
int fa[1048576], size[1048576];
long long fw[1048576];
bool ___ = false;
void findroot(int x) {
  if (fa[x] == x) {
    return;
  }
  findroot(fa[x]);
  if (___) {
    fw[x] += fw[fa[x]];
  } else {
    if (fw[fa[x]] > fw[x]) {
      fw[x] = fw[fa[x]];
    }
  }
  fa[x] = fa[fa[x]];
}
struct node {
  int tim;
  int tim2;
  bool operator<(node b) const { return tim < b.tim; }
};
node nn[524288];
long long ans[1048576];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = n + 1; i <= n + m; i++) {
    tp[i] = getchar();
    while (tp[i] < 'A' || tp[i] > 'Z') {
      tp[i] = getchar();
    }
    scanf("%d", &x[i]);
    if (tp[i] == 'U' || tp[i] == 'M') {
      scanf("%d", &y[i]);
    }
  }
  for (int i = 1; i <= n + m; i++) {
    fa[i] = i;
    fw[i] = 0;
  }
  int num = 0;
  for (int i = n + 1; i <= n + m; i++) {
    if (tp[i] == 'M') {
      findroot(x[i]);
      findroot(y[i]);
      int u = fa[x[i]];
      int v = fa[y[i]];
      fa[u] = i;
      fw[u] = 0;
      fa[v] = i;
      fw[v] = 0;
    } else if (tp[i] == 'Z') {
      findroot(x[i]);
      int u = fa[x[i]];
      fa[u] = i;
      fw[u] = i;
    } else if (tp[i] == 'Q') {
      findroot(x[i]);
      nn[num].tim = fw[x[i]];
      nn[num].tim2 = i;
      num++;
    }
  }
  sort(nn, nn + num);
  ___ = true;
  for (int i = 1; i <= n + m; i++) {
    fa[i] = i;
    fw[i] = 0;
    size[i] = 1;
  }
  int now = n;
  for (int i = 0; i < num; i++) {
    while (now < nn[i].tim) {
      now++;
      if (tp[now] == 'U') {
        findroot(x[now]);
        findroot(y[now]);
        int u = fa[x[now]];
        int v = fa[y[now]];
        fa[u] = now;
        fw[u] = 0;
        fa[v] = now;
        fw[v] = 0;
        size[now] = size[u] + size[v];
      } else if (tp[now] == 'A') {
        findroot(x[now]);
        int u = fa[x[now]];
        fa[u] = now;
        fw[u] = size[u];
        size[now] = size[u];
      } else if (tp[now] == 'Q') {
        findroot(x[now]);
        printf("%lld\n", fw[x[now]] - ans[now]);
      }
    }
    findroot(x[nn[i].tim2]);
    ans[nn[i].tim2] = fw[x[nn[i].tim2]];
  }
  while (now < n + m) {
    now++;
    if (tp[now] == 'U') {
      findroot(x[now]);
      findroot(y[now]);
      int u = fa[x[now]];
      int v = fa[y[now]];
      fa[u] = now;
      fw[u] = 0;
      fa[v] = now;
      fw[v] = 0;
      size[now] = size[u] + size[v];
    } else if (tp[now] == 'A') {
      findroot(x[now]);
      int u = fa[x[now]];
      fa[u] = now;
      fw[u] = size[u];
      size[now] = size[u];
    } else if (tp[now] == 'Q') {
      findroot(x[now]);
      printf("%lld\n", fw[x[now]] - ans[now]);
    }
  }
  return 0;
}