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;
const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0},  {0, -1},
                        {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
template <class T>
inline T abs1(T a) {
  return a < 0 ? -a : a;
}
template <typename t, typename t1>
t min1(t a, t1 b) {
  return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
  return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
  return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
  return max1(a, max1(arr...));
}
inline int jud(double a, double b) {
  if (abs(a) < eps && abs(b) < eps)
    return 0;
  else if (abs1(a - b) / abs1(a) < eps)
    return 0;
  if (a < b) return -1;
  return 1;
}
template <typename t>
inline int jud(t a, t b) {
  if (a < b) return -1;
  if (a == b) return 0;
  return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
  if (na == 0) return 0;
  int be = 0, en = na - 1;
  if (*a <= *(a + na - 1)) {
    if (f_lb == 0)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != 1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != -1)
          en = mid;
        else
          be = mid + 1;
      }
    if (f_small && jud(*(a + be), val) == 1) be--;
    if (!f_small && jud(*(a + be), val) == -1) be++;
  } else {
    if (f_lb)
      while (be < en) {
        int mid = (be + en + 1) / 2;
        if (jud(*(a + mid), val) != -1)
          be = mid;
        else
          en = mid - 1;
      }
    else
      while (be < en) {
        int mid = (be + en) / 2;
        if (jud(*(a + mid), val) != 1)
          en = mid;
        else
          be = mid + 1;
      }
    if (!f_small && jud(*(a + be), val) == -1) be--;
    if (f_small && jud(*(a + be), val) == 1) be++;
  }
  return be;
}
template <class T>
inline T lowb(T num) {
  return num & (-num);
}
inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(unsigned long long nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(long long nValue) {
  return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(unsigned int a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
  if (a == 0) return 0;
  return 32 - __builtin_clz(a);
}
inline int bitmaxl(unsigned long long a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
inline int bitmaxl(long long a) {
  int temp = a >> 32;
  if (temp) return 32 - __builtin_clz(temp) + 32;
  return bitmaxl(int(a));
}
long long pow(long long n, long long m, long long mod = 0) {
  if (m < 0) return 0;
  long long ans = 1;
  long long k = n;
  while (m) {
    if (m & 1) {
      ans *= k;
      if (mod) ans %= mod;
    }
    k *= k;
    if (mod) k %= mod;
    m >>= 1;
  }
  return ans;
}
template <class t1, class t2>
inline void add(t1 &a, t2 b, int mod = -1) {
  if (mod == -1) mod = 1000000007;
  a += b;
  while (a >= mod) a -= mod;
  while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
  cerr << endl;
}
template <class t>
void output2(t arr) {
  for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}
const int maxn = 501000;
vector<int> mil[maxn], uni[maxn];
int mbe[maxn], ube[maxn];
int fam[maxn], fau[maxn];
long long arr[maxn];
int opm[maxn], lastm[maxn];
vector<pair<long long, long long> > opu[maxn];
int n, nq;
int getfa(int no, int *fa) {
  if (fa[no] == no) return no;
  return fa[no] = getfa(fa[no], fa);
}
int unio(int a, int b, int *fa) {
  b = getfa(b, fa), a = getfa(a, fa);
  fa[b] = a;
  return 1;
}
long long get(int no) {
  int rm = max(lastm[no], opm[mbe[no]]);
  long long tt = arr[no];
  if (rm > lastm[no]) tt = 0;
  auto it1 = upper_bound(opu[ube[no]].begin(), opu[ube[no]].end(),
                         make_pair(rm * 1ll, -1ll));
  long long radd = 0;
  if (opu[ube[no]].size()) radd = opu[ube[no]][opu[ube[no]].size() - 1].second;
  if (it1 != opu[ube[no]].begin()) {
    it1--;
    radd -= it1->second;
  }
  return tt + radd;
}
int main() {
  ios_base::sync_with_stdio(0);
  scanf("%d%d", &n, &nq);
  for (int i = 0; i < n; i++) {
    mil[i].push_back(i), uni[i].push_back(i);
    opm[i] = lastm[i] = -1;
    mbe[i] = ube[i] = i;
    fau[i] = fam[i] = i;
  }
  for (int i = 0; i < nq; i++) {
    char cate[2];
    scanf("%s", cate);
    if (cate[0] == 'M') {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      a = getfa(a, fam), b = getfa(b, fam);
      if (mil[a].size() < mil[b].size()) swap(a, b);
      unio(a, b, fam);
      for (int j = 0; j < (int)mil[b].size(); j++) {
        int no = mil[b][j];
        arr[no] = get(no);
        lastm[no] = i;
        mbe[no] = a;
        mil[a].push_back(no);
      }
      mil[b].clear();
    } else if (cate[0] == 'U') {
      int a, b;
      scanf("%d%d", &a, &b);
      a--;
      b--;
      a = getfa(a, fau), b = getfa(b, fau);
      if (uni[a].size() < uni[b].size()) swap(a, b);
      unio(a, b, fau);
      for (int j = 0; j < (int)uni[b].size(); j++) {
        int no = uni[b][j];
        arr[no] = get(no);
        lastm[no] = i;
        ube[no] = a;
        uni[a].push_back(no);
      }
      uni[b].clear();
    } else if (cate[0] == 'A') {
      int a;
      scanf("%d", &a);
      a--;
      a = getfa(a, fau);
      if (opu[a].size()) {
        auto it = opu[a].rbegin();
        opu[a].push_back(make_pair(i, it->second + uni[a].size()));
      } else
        opu[a].push_back(make_pair(i, uni[a].size()));
    } else if (cate[0] == 'Z') {
      int a;
      scanf("%d", &a);
      a--;
      a = getfa(a, fam);
      opm[a] = i;
    } else {
      int a;
      scanf("%d", &a);
      a--;
      printf("%I64d\n", get(a));
    }
  }
  return 0;
}