571_D. Campus_8828
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. 
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Iterator;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            DCampus solver = new DCampus();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class DCampus {
        int indicator;

        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.ri();
            int m = in.ri();
            Node[] U = new Node[n];
            Node[] M = new Node[n];
            for (int i = 0; i < n; i++) {
                U[i] = new Node();
                M[i] = new Node();
            }
            Node[] cloneU = U.clone();
            Node[] cloneM = M.clone();
            Query[] queries = new Query[m];
            for (int i = 0; i < m; i++) {
                char c = in.rc();
                queries[i] = new Query();
                queries[i].t = c;
                queries[i].a = in.ri() - 1;
                if (c == 'U' || c == 'M') {
                    queries[i].b = in.ri() - 1;
                    if (c == 'U') {
                        Node node = new Node();
                        node.left = U[queries[i].a];
                        node.right = U[queries[i].b];
                        U[queries[i].a] = node;
                        U[queries[i].b] = null;
                    } else {
                        Node node = new Node();
                        node.left = M[queries[i].a];
                        node.right = M[queries[i].b];
                        M[queries[i].a] = node;
                        M[queries[i].b] = null;
                    }
                }
            }

            indicator = 0;
            for (Node node : U) {
                if (node == null) {
                    continue;
                }
                allocId(node);
            }
            indicator = 0;
            for (Node node : M) {
                if (node == null) {
                    continue;
                }
                allocId(node);
            }
            U = cloneU;
            M = cloneM;
            int[][] rangeU = new int[n][2];
            int[][] rangeM = new int[n][2];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < 2; j++) {
                    rangeU[i][j] = U[i].id;
                    rangeM[i][j] = M[i].id;
                }
            }

            RangeAffineRangeSum st = new RangeAffineRangeSum(0, n - 1);
            st.init(0, n - 1, i -> 0);
            int now = 0;

            MultiWayStack<Query> delete = new MultiWayStack<>(m + 1, m);
            MultiWayStack<Query> add = new MultiWayStack<>(m + 1, m);
            for (Query q : queries) {
                now++;
                if (q.t == 'U') {
                    //merge
                } else if (q.t == 'M') {
                    merge(rangeM[q.a], rangeM[q.b]);
                } else if (q.t == 'A') {
                } else if (q.t == 'Z') {
                    st.update(rangeM[q.a][0], rangeM[q.a][1], 0, n - 1, 0, now);
                } else if (q.t == 'Q') {
                    int lastPurgeTime = (int) st.query(M[q.a].id, M[q.a].id, 0, n - 1);
                    delete.addLast(lastPurgeTime, q);
                    add.addLast(now, q);
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < 2; j++) {
                    rangeU[i][j] = U[i].id;
                    rangeM[i][j] = M[i].id;
                }
            }
            st.init(0, n - 1, i -> 0);
            now = 0;

            for (Query q : queries) {
                now++;
                for (Query x : delete.getStack(now)) {
                    x.ans -= st.query(U[x.a].id, U[x.a].id, 0, n - 1);
                }
                for (Query x : add.getStack(now)) {
                    x.ans += st.query(U[x.a].id, U[x.a].id, 0, n - 1);
                }

                if (q.t == 'U') {
                    //merge
                    merge(rangeU[q.a], rangeU[q.b]);
                } else if (q.t == 'M') {
                } else if (q.t == 'A') {
                    int len = rangeU[q.a][1] - rangeU[q.a][0] + 1;
                    st.update(rangeU[q.a][0], rangeU[q.a][1], 0, n - 1, 1, len);
                } else if (q.t == 'Z') {
                } else if (q.t == 'Q') {
                    out.println(q.ans);
                }
            }

        }

        public void merge(int[] a, int[] b) {
            a[0] = Math.min(a[0], b[0]);
            a[1] = Math.max(a[1], b[1]);
        }

        public void allocId(Node root) {
            if (root.left == null) {
                root.id = indicator++;
                return;
            }
            allocId(root.left);
            allocId(root.right);
        }

    }

    static class Node {
        Node left;
        Node right;
        int id;

    }

    static class RangeAffineRangeSum implements Cloneable {
        private RangeAffineRangeSum left;
        private RangeAffineRangeSum right;
        private long sum;
        private long da;
        private long db;

        private void modify(long a, long b) {
            sum = (sum * a + b);
            da = (da * a);
            db = (db * a + b);
        }

        public void pushUp() {
        }

        public void pushDown() {
            if (!(da == 1 && db == 0)) {
                left.modify(da, db);
                right.modify(da, db);
                da = 1;
                db = 0;
            }
        }

        public RangeAffineRangeSum(int l, int r) {
            if (l < r) {
                int m = DigitUtils.floorAverage(l, r);
                left = new RangeAffineRangeSum(l, m);
                right = new RangeAffineRangeSum(m + 1, r);
                pushUp();
            }
        }

        public void init(int l, int r, IntToIntegerFunction function) {
            da = 1;
            db = 0;
            if (l < r) {
                int m = DigitUtils.floorAverage(l, r);
                left.init(l, m, function);
                right.init(m + 1, r, function);
                pushUp();
            } else {
                sum = function.apply(l);
            }
        }

        private boolean covered(int ll, int rr, int l, int r) {
            return ll <= l && rr >= r;
        }

        private boolean noIntersection(int ll, int rr, int l, int r) {
            return ll > r || rr < l;
        }

        public void update(int ll, int rr, int l, int r, long a, long b) {
            if (noIntersection(ll, rr, l, r)) {
                return;
            }
            if (covered(ll, rr, l, r)) {
                modify(a, b);
                return;
            }
            pushDown();
            int m = DigitUtils.floorAverage(l, r);
            left.update(ll, rr, l, m, a, b);
            right.update(ll, rr, m + 1, r, a, b);
            pushUp();
        }

        public long query(int ll, int rr, int l, int r) {
            if (noIntersection(ll, rr, l, r)) {
                return 0;
            }
            if (covered(ll, rr, l, r)) {
                return sum;
            }
            pushDown();
            int m = DigitUtils.floorAverage(l, r);
            return left.query(ll, rr, l, m) +
                    right.query(ll, rr, m + 1, r);
        }

        private RangeAffineRangeSum deepClone() {
            RangeAffineRangeSum seg = clone();
            if (seg.left != null) {
                seg.left = seg.left.deepClone();
            }
            if (seg.right != null) {
                seg.right = seg.right.deepClone();
            }
            return seg;
        }

        protected RangeAffineRangeSum clone() {
            try {
                return (RangeAffineRangeSum) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }

        private void toString(StringBuilder builder) {
            if (left == null && right == null) {
                builder.append(sum).append(",");
                return;
            }
            pushDown();
            left.toString(builder);
            right.toString(builder);
        }

        public String toString() {
            StringBuilder builder = new StringBuilder();
            deepClone().toString(builder);
            if (builder.length() > 0) {
                builder.setLength(builder.length() - 1);
            }
            return builder.toString();
        }

    }

    static class Query {
        char t;
        int a;
        int b;
        long ans;

    }

    static class DigitUtils {
        private DigitUtils() {
        }

        public static int floorAverage(int x, int y) {
            return (x & y) + ((x ^ y) >> 1);
        }

    }

    static interface SimplifiedStack<T> extends Iterable<T> {
    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int ri() {
            return readInt();
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public char rc() {
            return readChar();
        }

        public char readChar() {
            skipBlank();
            char c = (char) next;
            next = read();
            return c;
        }

    }

    static class MultiWayStack<T> {
        private Object[] values;
        private int[] next;
        private int[] heads;
        private int alloc;
        private int stackNum;

        public RevokeIterator<T> iterator(final int queue) {
            return new RevokeIterator<T>() {
                int ele = heads[queue];
                int pre = 0;


                public boolean hasNext() {
                    return ele != 0;
                }


                public T next() {
                    T ans = (T) values[ele];
                    pre = ele;
                    ele = next[ele];
                    return ans;
                }


                public void revoke() {
                    ele = pre;
                    pre = 0;
                }
            };
        }

        public SimplifiedStack<T> getStack(int qId) {
            return new SimplifiedStack<T>() {

                public boolean isEmpty() {
                    return MultiWayStack.this.isEmpty(qId);
                }


                public T peekLast() {
                    return MultiWayStack.this.peekLast(qId);
                }


                public void addLast(T x) {
                    MultiWayStack.this.addLast(qId, x);
                }


                public T removeLast() {
                    return MultiWayStack.this.removeLast(qId);
                }


                public Iterator<T> iterator() {
                    return MultiWayStack.this.iterator(qId);
                }
            };
        }

        private void doubleCapacity() {
            int newSize = Math.max(next.length + 10, next.length * 2);
            next = Arrays.copyOf(next, newSize);
            values = Arrays.copyOf(values, newSize);
        }

        public void alloc() {
            alloc++;
            if (alloc >= next.length) {
                doubleCapacity();
            }
            next[alloc] = 0;
        }

        public boolean isEmpty(int qId) {
            return heads[qId] == 0;
        }

        public MultiWayStack(int qNum, int totalCapacity) {
            values = new Object[totalCapacity + 1];
            next = new int[totalCapacity + 1];
            heads = new int[qNum];
            stackNum = qNum;
        }

        public void addLast(int qId, T x) {
            alloc();
            values[alloc] = x;
            next[alloc] = heads[qId];
            heads[qId] = alloc;
        }

        public T peekLast(int qId) {
            return (T) values[heads[qId]];
        }

        public T removeLast(int qId) {
            T ans = (T) values[heads[qId]];
            heads[qId] = next[heads[qId]];
            return ans;
        }

        public String toString() {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < stackNum; i++) {
                builder.append(i).append(": ");
                for (Iterator iterator = iterator(i); iterator.hasNext(); ) {
                    builder.append(iterator.next()).append(",");
                }
                if (builder.charAt(builder.length() - 1) == ',') {
                    builder.setLength(builder.length() - 1);
                }
                builder.append('\n');
            }
            return builder.toString();
        }

    }

    static interface RevokeIterator<E> extends Iterator<E> {
    }

    static class FastOutput implements AutoCloseable, Closeable, Appendable {
        private static final int THRESHOLD = 1 << 13;
        private final Writer os;
        private StringBuilder cache = new StringBuilder(THRESHOLD * 2);

        public FastOutput append(CharSequence csq) {
            cache.append(csq);
            return this;
        }

        public FastOutput append(CharSequence csq, int start, int end) {
            cache.append(csq, start, end);
            return this;
        }

        private void afterWrite() {
            if (cache.length() < THRESHOLD) {
                return;
            }
            flush();
        }

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput append(char c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput append(long c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput append(String c) {
            cache.append(c);
            afterWrite();
            return this;
        }

        public FastOutput println(long c) {
            return append(c).println();
        }

        public FastOutput println() {
            return append(System.lineSeparator());
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }

    static interface IntToIntegerFunction {
        int apply(int x);

    }
}

