Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.

After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.

The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1 to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.

Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types:

  * 1 i j — Place a book at position j at shelf i if there is no book at it.
  * 2 i j — Remove the book from position j at shelf i if there is a book at it.
  * 3 i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed.
  * 4 k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.



After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?

Input

The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively.

The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement.

It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.

Output

For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.

Examples

Input

2 3 3
1 1 1
3 2
4 0


Output

1
4
0


Input

4 2 6
3 2
2 2 2
3 3
3 2
2 2 2
3 2


Output

2
1
3
3
2
4


Input

2 2 2
3 2
2 2 1


Output

2
1

Note

<image>

This image illustrates the second sample case.
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.function.IntSupplier;
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);
            DPersistentBookcase solver = new DPersistentBookcase();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class DPersistentBookcase {
        static Bookcase bc;

        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.ri();
            int m = in.ri();
            int q = in.ri();
            bc = new Bookcase(n, m);
            OfflinePersistentTree pt = new OfflinePersistentTree(q);
            IntSupplier getAns = () -> bc.sum;
            OpExt[] op = new OpExt[q];
            for (int i = 0; i < q; i++) {
                int t = in.ri();
                if (t == 1) {
                    int a = in.ri() - 1;
                    int b = in.ri() - 1;
                    op[i] = new OpExt() {
                        int old;


                        public void apply() {
                            old = bc.set(a, b, 1);
                            ans = bc.sum;
                        }


                        public void undo() {
                            bc.set(a, b, old);
                        }
                    };
                } else if (t == 2) {
                    int a = in.ri() - 1;
                    int b = in.ri() - 1;
                    op[i] = new OpExt() {
                        int old;


                        public void apply() {
                            old = bc.set(a, b, 0);
                            ans = bc.sum;
                        }


                        public void undo() {
                            bc.set(a, b, old);
                        }
                    };
                } else if (t == 3) {
                    int a = in.ri() - 1;
                    op[i] = new OpExt() {

                        public void apply() {
                            bc.inv(a);
                            ans = bc.sum;
                        }


                        public void undo() {
                            bc.inv(a);
                        }
                    };
                } else {
                    op[i] = new OpExt() {

                        public void apply() {
                            ans = bc.sum;
                        }


                        public void undo() {

                        }
                    };
                    pt.switchVersion(in.ri());
                }
                pt.apply(op[i]);
            }
            pt.solve();
            for (OpExt ext : op) {
                out.println(ext.ans);
            }
        }

    }

    static abstract class OpExt implements UndoOperation {
        int ans;

    }

    static class OfflinePersistentTree {
        OfflinePersistentTree.Node[] nodes;
        int now = 0;
        int version = 0;

        public OfflinePersistentTree(int op) {
            nodes = new OfflinePersistentTree.Node[op + 1];
            nodes[0] = new OfflinePersistentTree.Node();
            nodes[0].operation = UndoOperation.NIL;
        }

        public void apply(UndoOperation op) {
            assert version <= now;
            nodes[++now] = new OfflinePersistentTree.Node();
            nodes[now].operation = op;
            nodes[version].adj.add(nodes[now]);
            version = now;
        }

        public void switchVersion(int v) {
            version = v;
        }

        public void solve() {
            dfs(nodes[0]);
        }

        private static void dfs(OfflinePersistentTree.Node root) {
            root.operation.apply();
            for (OfflinePersistentTree.Node node : root.adj) {
                dfs(node);
            }
            root.operation.undo();
        }

        private static class Node {
            List<OfflinePersistentTree.Node> adj = new ArrayList<>();
            UndoOperation operation;

        }

    }

    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(int c) {
            cache.append(c);
            afterWrite();
            return this;
        }

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

        public FastOutput println(int 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 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;
        }

    }

    static interface UndoOperation {
        static final UndoOperation NIL = new UndoOperation() {

            public void apply() {

            }


            public void undo() {

            }
        };

        void apply();

        void undo();

    }

    static class Bookcase {
        int[][] shelf;
        int[] xor;
        int[] size;
        int sum;

        public Bookcase(int n, int m) {
            shelf = new int[n][m];
            xor = new int[n];
            size = new int[n];
        }

        public int get(int i, int j) {
            return shelf[i][j] ^ xor[i];
        }

        public int set(int i, int j, int v) {
            int old = get(i, j);
            if (old == 1) {
                size[i]--;
                sum--;
            }
            shelf[i][j] = v ^ xor[i];
            if (v == 1) {
                size[i]++;
                sum++;
            }
            return old;
        }

        public void inv(int i) {
            xor[i] ^= 1;
            sum -= size[i];
            size[i] = shelf[i].length - size[i];
            sum += size[i];
        }

    }
}

