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>
#pragma comment(linker, "/STACK:128000000")
using namespace std;
void solve();
void precalc();
clock_t start;
bool doing = true;
int main() {
  int t = 1;
  cout.sync_with_stdio(0);
  cin.tie(0);
  precalc();
  cout.precision(10);
  cout << fixed;
  start = clock();
  int testNum = 1;
  while (t--) {
    solve();
  }
  return 0;
}
void precalc() {}
int binpow(int q, int w, int mod) {
  if (!w) return 1;
  if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod;
  return binpow(q * 1LL * q % mod, w / 2, mod);
}
int gcd(int q, int w) {
  while (w) {
    q %= w;
    swap(q, w);
  }
  return q;
}
struct _node;
typedef _node* node;
struct _node {
  int cnt, prior;
  _node* l;
  _node* r;
  _node* p;
  long long val;
  long long add;
  int lastNull;
  int lastNullToPush;
  _node() : val(0), add(0), lastNull(0), lastNullToPush(0) {
    prior = (rand() << 16) | rand();
    cnt = 1;
    l = nullptr;
    r = nullptr;
    p = nullptr;
  }
  inline void push() {
    if (l) {
      l->add += add;
      l->lastNullToPush = max(l->lastNullToPush, lastNullToPush);
    }
    if (r) {
      r->add += add;
      r->lastNullToPush = max(r->lastNullToPush, lastNullToPush);
    }
    lastNull = max(lastNull, lastNullToPush);
    val += add;
    add = 0;
    lastNullToPush = 0;
  }
  inline void recalc() {
    p = nullptr;
    cnt = 1 + Cnt(l) + Cnt(r);
    if (l) {
      l->p = this;
    }
    if (r) {
      r->p = this;
    }
  }
  static int Cnt(_node* v) {
    if (!v) return 0;
    return v->cnt;
  }
  node merge(node l, node r) {
    if (!l) return r;
    if (!r) return l;
    if (l->prior < r->prior) {
      l->push();
      l->r = merge(l->r, r);
      l->recalc();
      return l;
    } else {
      r->push();
      r->l = merge(l, r->l);
      r->recalc();
      return r;
    }
  }
  inline void split(node v, int key, node& l, node& r) {
    l = r = nullptr;
    if (!v) return;
    v->push();
    int cur = Cnt(v->l);
    if (cur < key) {
      l = v;
      split(l->r, key - cur - 1, l->r, r);
      l->recalc();
    } else {
      r = v;
      split(r->l, key, l, r->l);
      r->recalc();
    }
  }
  node push_back(node other) { return merge(this, other); }
  int getLastZero(node v) {
    int res = v->lastNull;
    while (v) {
      res = max(res, v->lastNullToPush);
      v = v->p;
    }
    return res;
  }
  long long getActualVal(_node* v) {
    long long res = v->val;
    while (v) {
      res += v->add;
      v = v->p;
    }
    return res;
  }
};
enum { UNI_MERGE = 0, WAR_MERGE = 1, UNI_ADD = 2, WAR_ZERO = 3, QUERY = 4 };
struct Query {
  int type;
  int q, w;
  int num;
  void scan() {
    char c;
    cin >> c;
    switch (c) {
      case 'U':
        type = UNI_MERGE;
        cin >> q >> w;
        --q;
        --w;
        break;
      case 'M':
        type = WAR_MERGE;
        cin >> q >> w;
        --q;
        --w;
        break;
      case 'A':
        type = UNI_ADD;
        cin >> q;
        --q;
        break;
      case 'Z':
        type = WAR_ZERO;
        cin >> q;
        --q;
        break;
      case 'Q':
        type = QUERY;
        cin >> q;
        --q;
        break;
      default:
        assert(false);
    }
  }
};
vector<int> dsu[2];
int findSet(int id, int v) {
  if (dsu[id][v] == v) {
    return v;
  }
  return dsu[id][v] = findSet(id, dsu[id][v]);
}
void merge(int id, int q, int w) { dsu[id][w] = q; }
void solve() {
  int n, Q;
  cin >> n >> Q;
  vector<Query> ask;
  vector<vector<pair<int, int>>> askNum(Q);
  for (int i = 0; i < Q; ++i) {
    Query cur;
    cur.scan();
    cur.num = i;
    if (cur.type == QUERY) {
      askNum[i].push_back(make_pair(cur.q, i));
    }
    ask.push_back(cur);
  }
  vector<long long> res(Q, 0);
  for (int dom = 1; dom >= -1; dom -= 2) {
    vector<int> sizes(n, 1);
    vector<node> uni(n), war(n);
    vector<node> uniRoots(n), warRoots(n);
    for (int i = 0; i < n; ++i) {
      uni[i] = new _node();
      war[i] = new _node();
      uniRoots[i] = uni[i];
      warRoots[i] = war[i];
    }
    for (int w = 0; w < 2; ++w) {
      dsu[w].resize(n);
      for (int i = 0; i < n; ++i) {
        dsu[w][i] = i;
      }
    }
    for (int i = 0; i < Q; ++i) {
      for (auto item : askNum[i]) {
        int id = item.first;
        long long curRes = uni[id]->getActualVal(uni[id]);
        res[item.second] += dom * curRes;
      }
      askNum[i].clear();
      auto& curAsk = ask[i];
      if (curAsk.type == QUERY) {
        int id = curAsk.q;
        int lastZero = war[id]->getLastZero(war[id]);
        askNum[lastZero].push_back(make_pair(id, i));
      }
      if (curAsk.type == UNI_MERGE) {
        uniRoots[curAsk.q] = uniRoots[curAsk.q]->push_back(uniRoots[curAsk.w]);
        sizes[curAsk.q] += sizes[curAsk.w];
        merge(0, curAsk.q, curAsk.w);
      }
      if (curAsk.type == WAR_MERGE) {
        warRoots[curAsk.q] = warRoots[curAsk.q]->push_back(warRoots[curAsk.w]);
        merge(1, curAsk.q, curAsk.w);
      }
      if (curAsk.type == UNI_ADD) {
        uniRoots[curAsk.q]->add += sizes[curAsk.q];
      }
      if (curAsk.type == WAR_ZERO) {
        warRoots[curAsk.q]->lastNullToPush = i;
      }
    }
  }
  for (int i = 0; i < Q; ++i) {
    if (ask[i].type == QUERY) {
      cout << res[i] << "\n";
    }
  }
}