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 JAVA):

Solution:
import java.io.*;
import java.util.*;

public class Main {
    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;
        
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
        
        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
    
    static class OutputWriter {
        PrintWriter writer;
        
        public OutputWriter(OutputStream stream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream), 32768));
        }
        
        public void println(int x) {
            writer.println(x);
        }
        
        public void println(long x) {
            writer.println(x);
        }
        
        public <T> void print(T x) {
            writer.print(x);
        }
        
        public <T> void println(T x) {
            writer.println(x);
        }
        
        public void close() {
            writer.close();
        }
    }
    
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        Task t = new Task();
        t.solve(in, out);
        out.close();
    }
    
    static class Task {
    
        static class AbstractGraphBuilder {
            int[] whereIsNode;
            int iterator;
            
            public AbstractGraphBuilder(int n, int m) {
                whereIsNode = new int[n + 1];
                iterator = n;
                for (int i = 1; i <= n; i++) {
                    whereIsNode[i] = i;
                }
            }
            
            public int askWhereIsNode(int x) {
                return whereIsNode[x];
            }
            
            public void union(int x, int y) {
                int z = (++iterator);
                whereIsNode[x] = z;
            }
            
            public int howManyVertices() {
                return iterator;
            }
            
            public void clear() {
                whereIsNode = null;
            }
        }
        
        static class GraphBuilder {
            AbstractGraphBuilder agb;
            ArrayList<Integer>[] adj;
            public int[] leafsCnt;
            public int[] subtreeSz;
            public int[] posInEuler;
            boolean[] used;
            int eulerIt;
            
            @SuppressWarnings("unchecked")
            public GraphBuilder(int n, int m) {
                int tmp = n + m + 1;
                adj = (ArrayList<Integer>[])new ArrayList[tmp];
                for (int i = 0; i < adj.length; i++) {
                    adj[i] = new ArrayList<Integer>();
                }
                agb = new AbstractGraphBuilder(n, m);
            }
            
            void addDirectedEdge(int x, int y) {
                adj[x].add(y);
            }
            
            public void union(int x, int y) {
                int a = agb.askWhereIsNode(x);
                int b = agb.askWhereIsNode(y);
                agb.union(x, y);
                int z = agb.askWhereIsNode(x);
                addDirectedEdge(z, a);
                addDirectedEdge(z, b);
            }
            
            public int askWhereIsNode(int x) {
                return agb.askWhereIsNode(x);
            }
            
            public int howManyVertices() {
                return agb.howManyVertices();
            }
            
            void dfs(int v, int p) {
                used[v] = true;
                subtreeSz[v] = 1;
                posInEuler[v] = eulerIt++;
                leafsCnt[v] = 0;
                
                for (int to : adj[v]) {
                    if (to == p) continue;
                    dfs(to, v);
                    subtreeSz[v] += subtreeSz[to];
                    leafsCnt[v] += leafsCnt[to];
                }
                
                if (subtreeSz[v] == 1) leafsCnt[v] = 1;
            }
            
            void clear() {
                agb.clear();
                used = null;
                adj = null;
            }
            
            public void doCalculations() {
                int n = howManyVertices();
                eulerIt = 0;
                leafsCnt = new int[n + 1];
                subtreeSz = new int[n + 1];
                posInEuler = new int[n + 1];
                used = new boolean[n + 1];
                
                for (int v = n; v >= 1; v--) {
                    if (!used[v]) {
                        dfs(v, -1);
                    }
                }
                
                clear();
            }
        }
        
        static class Query {
            final char type;
            final int x;
            final int y;
            
            public Query(char type, int x, int y) {
                this.type = type;
                this.x = x;
                this.y = y;
            }
        }
        
        static class AskValueQuery {
            int x;
            long result;
            AskValueQuery(int x) {
                this.x = x;
            }
            public int getX() {
                return x;
            }
            public void setResult(long result) {
                this.result = result;
            }
            public long getResult() {
                return result;
            }
        }
        
        static class FullAskQuery {
            AskValueQuery l, r;
            public FullAskQuery(AskValueQuery l, AskValueQuery r) {
                this.l = l;
                this.r = r;
            }
            public long getResult() {
                return r.getResult() - l.getResult();
            }
        }
        
        Query[] queries;
        ArrayList<AskValueQuery>[] askValueQueries;
        FullAskQuery[] fullAskQueries;
        GraphBuilder university, military;
        int[] militaryTree;
        
        void buildMilitaryTree() {
            militaryTree = new int[military.howManyVertices() * 4];
        }
        
        void pushInMilitaryTree(int v) {
            int l = v * 2 + 1;
            int r = v * 2 + 2;
            if (militaryTree[l] < militaryTree[v]) {
                militaryTree[l] = militaryTree[v];
            }
            if (militaryTree[r] < militaryTree[v]) {
                militaryTree[r] = militaryTree[v];
            }
        }
        
        void raidToMilitaryTreeRec(int v, int tl, int tr, int l, int r, int idx) {
            if (tl == l && tr == r) {
                militaryTree[v] = idx;
                return;
            }
            pushInMilitaryTree(v);
            int tm = (tl + tr) / 2;
            if (r <= tm) {
                raidToMilitaryTreeRec(v * 2 + 1, tl, tm, l, r, idx);
            }
            else if (l > tm) {
                raidToMilitaryTreeRec(v * 2 + 2, tm + 1, tr, l, r, idx);
            }
            else {
                raidToMilitaryTreeRec(v * 2 + 1, tl, tm, l, tm, idx);
                raidToMilitaryTreeRec(v * 2 + 2, tm + 1, tr, tm + 1, r, idx);
            }
        }
        
        void raidToMilitaryTree(int l, int r, int idx) {
            raidToMilitaryTreeRec(0, 0, military.howManyVertices() - 1, l, r, idx);
        }
        
        int militaryTreeGet(int v, int tl, int tr, int pos) {
            if (tl == tr) {
                return militaryTree[v];
            }
            pushInMilitaryTree(v);
            int tm = (tl + tr) / 2;
            if (pos <= tm) {
                return militaryTreeGet(v * 2 + 1, tl, tm, pos);
            }
            else {
                return militaryTreeGet(v * 2 + 2, tm + 1, tr, pos);
            }
        }
        
        int getIdxOfLastRaid(int x) {
            return militaryTreeGet(0, 0, military.howManyVertices() - 1, military.posInEuler[x]);
        }
        
        long[] universityTree;
        
        void buildUniversityTree() {
            universityTree = new long[university.howManyVertices() * 4];
        }
        
        void addToUniversityTreeRec(int v, int tl, int tr, int l, int r, long val) {
            if (tl == l && tr == r) {
                universityTree[v] += val;
                return;
            }
            int tm = (tl + tr) / 2;
            if (r <= tm) {
                addToUniversityTreeRec(v * 2 + 1, tl, tm, l, r, val);
            }
            else if (l > tm) {
                addToUniversityTreeRec(v * 2 + 2, tm + 1, tr, l, r, val);
            }
            else {
                addToUniversityTreeRec(v * 2 + 1, tl, tm, l, tm, val);
                addToUniversityTreeRec(v * 2 + 2, tm + 1, tr, tm + 1, r, val);
            }
        }
        
        long universityTreeGet(int v, int tl, int tr, int pos) {
            if (tl == tr) {
                return universityTree[v];
            }
            int tm = (tl + tr) / 2;
            if (pos <= tm) {
                return universityTree[v] + universityTreeGet(v * 2 + 1, tl, tm, pos);
            }
            else {
                return universityTree[v] + universityTreeGet(v * 2 + 2, tm + 1, tr, pos);
            }
        }
        
        long getValueInUniversity(int x) {
            return universityTreeGet(0, 0, university.howManyVertices() - 1, university.posInEuler[x]);
        }
        
        void addToUniversityTree(int l, int r, long val) {
            addToUniversityTreeRec(0, 0, university.howManyVertices() - 1, l, r, val);
        }
        
        @SuppressWarnings("unchecked")
        public void solve(InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            
            queries = new Query[m + 1];
            askValueQueries = (ArrayList<AskValueQuery>[])new ArrayList[m + 1];
            for (int i = 0; i < askValueQueries.length; i++) {
                askValueQueries[i] = new ArrayList<AskValueQuery>();
            }
            fullAskQueries = new FullAskQuery[m + 1];
            
            for (int i = 1; i <= m; i++) {
                char type = in.next().charAt(0);
                int x = 0, y = 0;
                if (type == 'U' || type == 'M') {
                    x = in.nextInt();
                    y = in.nextInt();
                }
                else {
                    x = in.nextInt();
                }
                queries[i] = new Query(type, x, y);
            }
            
            university = new GraphBuilder(n, m);
            
            for (int i = 1; i <= m; i++) {
                if (queries[i].type == 'U') {
                    university.union(queries[i].x, queries[i].y);
                }
            }
            
            university.doCalculations();
            
            military = new GraphBuilder(n, m);
            
            for (int i = 1; i <= m; i++) {
                if (queries[i].type == 'M') {
                    military.union(queries[i].x, queries[i].y);
                }
            }
            
            military.doCalculations();
            
            buildMilitaryTree();
        
            AbstractGraphBuilder abstractMilitary = new AbstractGraphBuilder(n, m);
            
            for (int i = 1; i <= m; i++) {
                if (queries[i].type == 'M') {
                    abstractMilitary.union(queries[i].x, queries[i].y);
                }
                else if (queries[i].type == 'Z') {
                    int x = abstractMilitary.askWhereIsNode(queries[i].x);
                    raidToMilitaryTree(military.posInEuler[x], military.posInEuler[x] + military.subtreeSz[x] - 1, i);
                }
                else if (queries[i].type == 'Q') {
                    int x = queries[i].x;
                    int idxOfLastRaid = getIdxOfLastRaid(queries[i].x);
                    askValueQueries[idxOfLastRaid].add(new AskValueQuery(x));
                    askValueQueries[i].add(new AskValueQuery(x));
                    fullAskQueries[i] = new FullAskQuery(askValueQueries[idxOfLastRaid].get(askValueQueries[idxOfLastRaid].size() - 1),
                                                        askValueQueries[i].get(askValueQueries[i].size() - 1));
                }
            }
            
            buildUniversityTree();
            
            AbstractGraphBuilder abstractUniversity = new AbstractGraphBuilder(n, m);
            
            for (int i = 1; i <= m; i++) {
                if (queries[i].type == 'U') {
                    abstractUniversity.union(queries[i].x, queries[i].y);
                }
                else if (queries[i].type == 'A') {
                    int x = abstractUniversity.askWhereIsNode(queries[i].x);
                    addToUniversityTree(university.posInEuler[x], university.posInEuler[x] + university.subtreeSz[x] - 1, university.leafsCnt[x]);
                }
                for (AskValueQuery q : askValueQueries[i]) {
                    q.setResult(getValueInUniversity(q.getX()));
                }
            }
            
            for (FullAskQuery q : fullAskQueries) {
                if (q != null) {
                    out.println(q.getResult());
                }
            }
            
            out.close();
        }
    }
}