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.util.SortedSet;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.io.OutputStreamWriter;
import java.util.NoSuchElementException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.Set;
import java.io.IOException;
import java.util.NavigableSet;
import java.util.List;
import java.io.Writer;
import java.util.Comparator;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        int n;
        int m;
        int q;
        PersistentTreapSet<TaskD.Key>[] idRows;
        TaskD.BitSet[] rows;
        int[] answer;

        public void solve(int testNumber, InputReader in, OutputWriter out) {

            n = in.readInt();
            m = in.readInt();
            q = in.readInt();

            int size = 0;
            rows = new TaskD.BitSet[q + 1];
            rows[size++] = new TaskD.BitSet(m);

            idRows = new PersistentTreapSet[q + 1];
            idRows[0] = new PersistentTreapSet<>();
            for (int i = 0; i < n; i++) {
                idRows[0].add(new TaskD.Key(i, 0));
            }

            answer = new int[q + 1];
            answer[0] = 0;

//        ExtendedRandom rnd = new ExtendedRandom(123);
            for (int i = 1; i <= q; i++) {
                int op = in.readInt();
                PersistentTreapSet<TaskD.Key> set;

                if (op <= 3) {
                    idRows[i - 1].markState(0);
                    set = idRows[i - 1].getState(0);
                    answer[i] = answer[i - 1];

                    int row = in.readInt() - 1;
                    int col = op <= 2 ? in.readInt() - 1 : -1;

                    int itemIndex = set.get(row).itemIndex;
                    TaskD.BitSet item = rows[itemIndex].clon();
                    if (op == 1) {
                        if (!item.get(col)) {
                            item.set(col);
                            ++answer[i];
                        }
                    } else if (op == 2) {
                        if (item.get(col)) {
                            item.clear(col);
                            --answer[i];
                        }
                    } else if (op == 3) {
                        int delta = 0;
                        for (int j = 0; j < m; j++) {
                            if (item.get(j)) {
                                --delta;
                                item.clear(j);
                            } else {
                                ++delta;
                                item.set(j);
                            }
                        }
                        answer[i] += delta;
                    }
                    set.remove(set.get(row));
                    set.add(new TaskD.Key(row, size));
                    rows[size++] = item;
                } else {
                    int k = in.readInt();
                    idRows[k].markState(0);
                    set = idRows[k].getState(0);
                    answer[i] = answer[k];
                }
                idRows[i] = set;

                // AmyLeeStone
                out.printLine(answer[i]);
            }
        /**/
        }

        static class BitSet {
            static final long msk64 = 63;
            long[] words;

            BitSet(int m) {
                words = new long[(m + 63) >> 6];
            }

            BitSet() {

            }

            TaskD.BitSet clon() {
                TaskD.BitSet bitSet = new TaskD.BitSet();
                bitSet.words = words.clone();
                return bitSet;
            }

            public boolean get(int i) {
                return (words[i >> 6] & (1L << (i & msk64))) != 0;
            }

            public void set(int i) {
                words[i >> 6] |= 1L << (i & msk64);
            }

            void clear(int i) {
                words[i >> 6] &= ~(1L << (i & msk64));
            }

        }

        static class Key implements Comparable<TaskD.Key> {
            int index;
            int itemIndex;

            public Key(int index, int itemIndex) {
                this.index = index;
                this.itemIndex = itemIndex;
            }


            public int compareTo(TaskD.Key o) {
                return index - o.index;
            }

        }

    }

    static class TreapSet<K> implements NavigableSet<K> {
        protected static final Random rnd = new Random(239);
        protected final Node nullNode;
        protected final Comparator<? super K> comparator;
        protected Node root;
        private final K from;
        private final K to;
        private final boolean fromInclusive;
        private final boolean toInclusive;

        public TreapSet() {
            this((Comparator<? super K>) null);
        }

        public TreapSet(Comparator<? super K> comparator) {
            this(null, null, false, false, comparator, null, null);
        }

        public TreapSet(Iterable<? extends K> collection) {
            this(collection, null);
        }

        public TreapSet(Iterable<? extends K> collection, Comparator<? super K> comparator) {
            this(comparator);
            addAll(collection);
        }

        protected TreapSet(K from, K to, boolean fromInclusive, boolean toInclusive, Comparator<? super K> comparator,
                           Node root, Node nullNode) {
            this.comparator = comparator;
            this.from = from;
            this.to = to;
            this.fromInclusive = fromInclusive;
            this.toInclusive = toInclusive;
            if (nullNode == null) {
                nullNode = new NullNode();
            }
            if (root == null) {
                root = nullNode;
            }
            this.root = root;
            this.nullNode = nullNode;
        }

        public boolean addAll(Iterable<? extends K> collection) {
            boolean result = false;
            for (K element : collection) {
                result |= add(element);
            }
            return result;
        }

        public K lower(K k) {
            Node target = root.lower(k);
            if (target == nullNode) {
                return null;
            }
            if (belongs(target.key)) {
                return target.key;
            }
            return null;
        }

        private boolean belongs(K key) {
            int valueFrom = compare(from, key);
            int valueTo = compare(key, to);
            return ((valueFrom < 0) || ((valueFrom == 0) && fromInclusive)) &&
                    ((valueTo < 0) || ((valueTo == 0) && toInclusive));
        }

        public K floor(K k) {
            Node target = root.floor(k);
            if (target == nullNode) {
                return null;
            }
            if (belongs(target.key)) {
                return target.key;
            }
            return null;
        }

        public K ceiling(K k) {
            Node target = root.ceil(k);
            if (target == nullNode) {
                return null;
            }
            if (belongs(target.key)) {
                return target.key;
            }
            return null;
        }

        public K higher(K k) {
            Node target = root.higher(k);
            if (target == nullNode) {
                return null;
            }
            if (belongs(target.key)) {
                return target.key;
            }
            return null;
        }

        public K pollFirst() {
            K first = first();
            if (first == null) {
                throw new NoSuchElementException();
            }
            root.erase(first);
            return first;
        }

        public K pollLast() {
            K last = last();
            if (last == null) {
                throw new NoSuchElementException();
            }
            root.erase(last);
            return last;
        }

        public int size() {
            if ((from == null) && (to == null)) {
                return root.size;
            }
            if (from == null) {
                Node to = toInclusive ? root.floor(this.to) : root.lower(this.to);
                if (to == nullNode) {
                    return 0;
                }
                return root.indexOf(to) + 1;
            }
            if (to == null) {
                Node from = fromInclusive ? root.ceil(this.from) : root.higher(this.from);
                if (from == nullNode) {
                    return 0;
                }
                return root.size - root.indexOf(from);
            }
            Node from = fromInclusive ? root.ceil(this.from) : root.higher(this.from);
            if ((from == nullNode) || !belongs(from.key)) {
                return 0;
            }
            Node to = toInclusive ? root.floor(this.to) : root.lower(this.to);
            return (root.indexOf(to) - root.indexOf(from)) + 1;
        }

        public boolean isEmpty() {
            return size() == 0;
        }

        @SuppressWarnings({"unchecked"})
        public boolean contains(Object o) {
            return belongs((K) o) && (root.search((K) o) != nullNode);
        }

        public Iterator<K> iterator() {
            return new Iterator<K>() {
                private K current = first();

                public boolean hasNext() {
                    return current != null;
                }

                public K next() {
                    K result = current;
                    current = higher(current);
                    return result;
                }

                public void remove() {
                    TreapSet.this.remove(current);
                }
            };
        }

        public Object[] toArray() {
            Object[] array = new Object[size()];
            int index = 0;
            for (K key : this) {
                array[index++] = key;
            }
            return array;
        }

        @SuppressWarnings({"unchecked"})
        public <T> T[] toArray(T[] a) {
            if (a.length < size()) {
                throw new IllegalArgumentException();
            }
            int index = 0;
            for (K key : this) {
                a[index++] = (T) key;
            }
            return a;
        }

        public boolean add(K k) {
            if (k == null) {
                throw new NullPointerException();
            }
            if (contains(k)) {
                return false;
            }
            root = root.insert(createNode(k));
            return true;
        }

        protected Node createNode(K k) {
            return new Node(k, rnd.nextLong());
        }

        public boolean remove(Object o) {
            if (!contains(o)) {
                return false;
            }
            //noinspection unchecked
            root = root.erase((K) o);
            return true;
        }

        public boolean containsAll(Collection<?> c) {
            for (Object o : c) {
                if (!contains(o)) {
                    return false;
                }
            }
            return true;
        }

        public boolean addAll(Collection<? extends K> c) {
            return addAll((Iterable<? extends K>) c);
        }

        public boolean retainAll(Collection<?> c) {
            List<K> toRemove = new ArrayList<K>();
            for (K key : this) {
                if (!c.contains(key)) {
                    toRemove.add(key);
                }
            }
            return removeAll(toRemove);
        }

        public boolean removeAll(Collection<?> c) {
            boolean result = false;
            for (Object o : c) {
                result |= remove(o);
            }
            return result;
        }

        public void clear() {
            retainAll(Collections.<Object>emptySet());
        }

        public NavigableSet<K> descendingSet() {
            throw new UnsupportedOperationException();
        }

        public Iterator<K> descendingIterator() {
            return new Iterator<K>() {
                private K current = last();

                public boolean hasNext() {
                    return current != null;
                }

                public K next() {
                    K result = current;
                    current = lower(current);
                    return result;
                }

                public void remove() {
                    TreapSet.this.remove(current);
                }
            };
        }

        public NavigableSet<K> subSet(K fromElement, boolean fromInclusive, K toElement, boolean toInclusive) {
            return new TreapSet<K>(fromElement, toElement, fromInclusive, toInclusive, comparator, root, nullNode);
        }

        public NavigableSet<K> headSet(K toElement, boolean inclusive) {
            return subSet(null, false, toElement, inclusive);
        }

        public NavigableSet<K> tailSet(K fromElement, boolean inclusive) {
            return subSet(fromElement, inclusive, null, false);
        }

        public Comparator<? super K> comparator() {
            return comparator;
        }

        public SortedSet<K> subSet(K fromElement, K toElement) {
            return subSet(fromElement, true, toElement, false);
        }

        public SortedSet<K> headSet(K toElement) {
            return subSet(null, false, toElement, false);
        }

        public SortedSet<K> tailSet(K fromElement) {
            return tailSet(fromElement, true);
        }

        public K first() {
            if (isEmpty()) {
                return null;
            }
            if (from == null) {
                return root.first().key;
            }
            if (fromInclusive) {
                return ceiling(from);
            }
            return higher(from);
        }

        public K last() {
            if (isEmpty()) {
                return null;
            }
            if (to == null) {
                return root.last().key;
            }
            if (toInclusive) {
                return floor(to);
            }
            return lower(to);
        }

        public K get(int index) {
            if ((index >= size()) || (index < 0)) {
                throw new IndexOutOfBoundsException(Integer.toString(index));
            }
            if (from != null) {
                index += headSet(from, !fromInclusive).size();
            }
            return root.get(index);
        }

        protected int compare(K first, K second) {
            if ((first == null) || (second == null)) {
                return -1;
            }
            if (comparator != null) {
                return comparator.compare(first, second);
            }
            //noinspection unchecked
            return ((Comparable<? super K>) first).compareTo(second);
        }

        protected class Node {
            protected final K key;
            protected final long priority;
            protected Node left;
            protected Node right;
            protected int size;

            protected Node(K key, long priority) {
                this.key = key;
                this.priority = priority;
                left = nullNode;
                right = nullNode;
                size = 1;
            }

            @SuppressWarnings({"unchecked"})
            protected Object[] split(K key) {
                if (compare(key, this.key) < 0) {
                    Object[] result = left.split(key);
                    left = (Node) result[1];
                    result[1] = this;
                    updateSize();
                    return result;
                }
                Object[] result = right.split(key);
                right = (Node) result[0];
                result[0] = this;
                updateSize();
                return result;
            }

            protected void updateSize() {
                size = left.size + right.size + 1;
            }

            @SuppressWarnings({"unchecked"})
            protected Node insert(Node node) {
                if (node.priority > priority) {
                    Object[] result = split(node.key);
                    node.left = (Node) result[0];
                    node.right = (Node) result[1];
                    node.updateSize();
                    return node;
                }
                if (compare(node.key, this.key) < 0) {
                    left = left.insert(node);
                    updateSize();
                    return this;
                }
                right = right.insert(node);
                updateSize();
                return this;
            }

            protected Node merge(Node left, Node right) {
                if (left == nullNode) {
                    return right;
                }
                if (right == nullNode) {
                    return left;
                }
                if (left.priority > right.priority) {
                    left.right = left.right.merge(left.right, right);
                    left.updateSize();
                    return left;
                }
                right.left = right.left.merge(left, right.left);
                right.updateSize();
                return right;
            }

            protected Node erase(K key) {
                int value = compare(key, this.key);
                if (value == 0) {
                    return merge(left, right);
                }
                if (value < 0) {
                    left = left.erase(key);
                    updateSize();
                    return this;
                }
                right = right.erase(key);
                updateSize();
                return this;
            }

            protected Node lower(K key) {
                if (compare(key, this.key) <= 0) {
                    return left.lower(key);
                }
                Node result = right.lower(key);
                if (result == nullNode) {
                    return this;
                }
                return result;
            }

            protected Node floor(K key) {
                if (compare(key, this.key) < 0) {
                    return left.floor(key);
                }
                Node result = right.floor(key);
                if (result == nullNode) {
                    return this;
                }
                return result;
            }

            protected Node higher(K key) {
                if (compare(key, this.key) >= 0) {
                    return right.higher(key);
                }
                Node result = left.higher(key);
                if (result == nullNode) {
                    return this;
                }
                return result;
            }

            protected Node ceil(K key) {
                if (compare(key, this.key) > 0) {
                    return right.ceil(key);
                }
                Node result = left.ceil(key);
                if (result == nullNode) {
                    return this;
                }
                return result;
            }

            protected Node first() {
                if (left == nullNode) {
                    return this;
                }
                return left.first();
            }

            protected Node last() {
                if (right == nullNode) {
                    return this;
                }
                return right.last();
            }

            protected Node search(K key) {
                int value = compare(key, this.key);
                if (value == 0) {
                    return this;
                }
                if (value < 0) {
                    return left.search(key);
                }
                return right.search(key);
            }

            public int indexOf(Node node) {
                if (this == node) {
                    return left.size;
                }
                if (compare(node.key, this.key) > 0) {
                    return left.size + 1 + right.indexOf(node);
                }
                return left.indexOf(node);
            }

            public K get(int index) {
                if (index < left.size) {
                    return left.get(index);
                } else if (index == left.size) {
                    return key;
                } else {
                    return right.get(index - left.size - 1);
                }
            }

        }

        private class NullNode extends Node {
            private Object[] splitResult = new Object[2];

            private NullNode() {
                super(null, Long.MIN_VALUE);
                left = this;
                right = this;
                size = 0;
            }


            protected Object[] split(K key) {
                splitResult[0] = splitResult[1] = this;
                return splitResult;
            }


            protected Node insert(Node node) {
                return node;
            }


            protected Node erase(K key) {
                return this;
            }


            protected Node lower(K key) {
                return this;
            }


            protected Node floor(K key) {
                return this;
            }


            protected Node higher(K key) {
                return this;
            }


            protected Node ceil(K key) {
                return this;
            }


            protected Node first() {
                throw new NoSuchElementException();
            }


            protected Node last() {
                throw new NoSuchElementException();
            }


            protected void updateSize() {
            }


            protected Node search(K key) {
                return this;
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if ((c < '0') || (c > '9')) {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return (c == ' ') || (c == '\n') || (c == '\r') || (c == '\t') || (c == -1);
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static interface PersistentSet<K> extends NavigableSet<K> {
    }

    static class PersistentTreapSet<K> extends TreapSet<K> implements PersistentSet<K> {
        private Map<Object, Node> states = new HashMap<Object, Node>();

        public PersistentTreapSet() {
            super();
        }

        public PersistentTreapSet(Comparator<? super K> comparator) {
            super(comparator);
        }

        public PersistentTreapSet(Iterable<? extends K> collection) {
            super(collection);
        }

        public PersistentTreapSet(Iterable<? extends K> collection, Comparator<? super K> comparator) {
            super(collection, comparator);
        }

        public PersistentTreapSet(Node root, Comparator<? super K> comparator, Node nullNode) {
            super(null, null, false, false, comparator, root, nullNode);
        }

        public void markState(Object marker) {
            //noinspection unchecked
            states.put(marker, root);
        }

        public PersistentTreapSet<K> getState(Object marker) {
            return new PersistentTreapSet<K>(states.get(marker), comparator, nullNode);
        }


        protected Node createNode(K k) {
            return new PersistentNode(k, TreapSet.rnd.nextLong());
        }

        protected class PersistentNode extends Node {
            protected PersistentNode(K key, long priority) {
                super(key, priority);
            }

            @SuppressWarnings({"CloneDoesntCallSuperClone", "CloneDoesntDeclareCloneNotSupportedException"})

            protected Node clone() {
                Node clone = new PersistentNode(key, priority);
                clone.left = left;
                clone.right = right;
                clone.size = size;
                return clone;
            }

            @SuppressWarnings({"unchecked"})
            protected Object[] split(K key) {
                Node clone = clone();
                if (compare(key, this.key) < 0) {
                    Object[] result = left.split(key);
                    clone.left = (Node) result[1];
                    result[1] = clone;
                    clone.updateSize();
                    return result;
                }
                Object[] result = right.split(key);
                clone.right = (Node) result[0];
                result[0] = clone;
                clone.updateSize();
                return result;
            }

            @SuppressWarnings({"unchecked"})
            protected Node insert(Node node) {
                if (node.priority > priority) {
                    Object[] result = split(node.key);
                    node.left = (Node) result[0];
                    node.right = (Node) result[1];
                    node.updateSize();
                    return node;
                }
                Node clone = clone();
                if (compare(node.key, this.key) < 0) {
                    clone.left = left.insert(node);
                    clone.updateSize();
                    return clone;
                }
                clone.right = right.insert(node);
                clone.updateSize();
                return clone;
            }

            protected Node merge(Node left, Node right) {
                if (left == nullNode) {
                    return right;
                }
                if (right == nullNode) {
                    return left;
                }
                if (left.priority > right.priority) {
                    //noinspection unchecked
                    left = ((PersistentNode) left).clone();
                    left.right = left.right.merge(left.right, right);
                    left.updateSize();
                    return left;
                }
                //noinspection unchecked
                right = ((PersistentNode) right).clone();
                right.left = right.left.merge(left, right.left);
                right.updateSize();
                return right;
            }

            protected Node erase(K key) {
                int value = compare(key, this.key);
                if (value == 0) {
                    return merge(left, right);
                }
                Node clone = clone();
                if (value < 0) {
                    clone.left = left.erase(key);
                    clone.updateSize();
                    return clone;
                }
                clone.right = right.erase(key);
                clone.updateSize();
                return clone;
            }

        }

    }
}

