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.stream.IntStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.stream.Stream;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.BitSet;
import java.io.BufferedReader;
import java.io.InputStream;

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

    static class DPersistentBookcase {
        public void solve(int testNumber, LightScanner in, LightWriter out) {
            int n = in.ints(), m = in.ints(), queries = in.ints();
            BitSet[] sets = IntStream.range(0, n).mapToObj(x -> new BitSet(m)).toArray(BitSet[]::new);
            PersistentArray<BitSet> array = new PersistentArray<>(sets);
            int[] ans = new int[queries + 1];
            int count = 0;
            for (int q = 0; q < queries; q++) {
                switch (in.ints()) {
                    case 1: {
                        int i = in.ints() - 1, j = in.ints() - 1;
                        BitSet set = (BitSet) array.get(i).clone();
                        if (!set.get(j)) {
                            count++;
                        }
                        set.set(j);
                        array.set(i, set);
                    }
                    break;
                    case 2: {
                        int i = in.ints() - 1, j = in.ints() - 1;
                        BitSet set = (BitSet) array.get(i).clone();
                        if (set.get(j)) {
                            count--;
                        }
                        set.clear(j);
                        array.set(i, set);
                    }
                    break;
                    case 3: {
                        int i = in.ints() - 1;
                        BitSet set = (BitSet) array.get(i).clone();
                        count += m - 2 * set.cardinality();
                        set.flip(0, m);
                        array.set(i, set);
                    }
                    break;
                    case 4: {
                        int k = in.ints();
                        count = ans[k];
                        array.set(k);
                    }
                    break;
                }
                ans[q + 1] = count;
                out.ansln(count);
            }
        }

    }

    static class PersistentArray<T> {
        private static final int BASE = 4;
        private static final int MASK = (1 << BASE) - 1;
        private final int n;
        private final int height;
        private int lastUpdate = 0;
        private int[] roots;
        private int newElements = 0;
        private int[][] nodes;
        private T[] elements;

        public PersistentArray(T[] array, int initialTimeCapacity, int initialNodeCapacity) {
            if (array.length == 0) {
                throw new IllegalArgumentException("Empty array is not supported");
            } else if (initialTimeCapacity < 1 || initialNodeCapacity < 1) {
                throw new IllegalArgumentException("Initial capacity must be a positive number");
            }
            n = array.length;
            int log = 1;
            while (n > (1 << (log * BASE))) {
                log++;
            }
            height = log - 1;

            roots = new int[1 + initialTimeCapacity];
            nodes = new int[1 << BASE][nodes(height) + initialNodeCapacity * (height + 1)];
            elements = Arrays.copyOf(array, n + initialNodeCapacity);
            int i = 0;
            for (; i < nodes(height - 1); i++) {
                for (int j = 0; j < (1 << BASE); j++) {
                    nodes[j][i] = (1 << BASE) * i + j + 1;  // 子ノードへのリンク
                }
            }
            int j = 0;
            for (; j < n; j++) {
                nodes[j & MASK][i + (j >> BASE)] = j;       // 実要素へのリンク
            }
            for (; i + (j >> BASE) < nodes(height); j++) {
                nodes[j & MASK][i + (j >> BASE)] = -1;      // 番兵
            }
            newElements = 0;
        }

        public PersistentArray(T[] array) {
            this(array, 1, 1);
        }

        public T get(int i, int t) {
            if (i < 0 || n <= i) {
                throw new ArrayIndexOutOfBoundsException("Index " + i + " is out of bounds");
            } else if (t < 0 || lastUpdate < t) {
                throw new TimeOutOfBoundsException("Time " + t + " is out of bounds");
            }
            int c = roots[t];
            for (int ss = BASE * height; ss >= 0; ss -= BASE) {
                c = nodes[(i >>> ss) & MASK][c];
            }
            return elements[c];
        }

        public T get(int i) {
            return get(i, lastUpdate);
        }

        private void growRoots(int minCapacity) {
            int capacity = roots.length - 1;
            while (capacity < minCapacity) {
                capacity <<= 1;
            }
            roots = Arrays.copyOf(roots, 1 + capacity);
        }

        private void growNodes(int minCapacity) {
            int capacity = elements.length - n;
            while (capacity < minCapacity) {
                capacity <<= 1;
            }
            for (int i = 0; i < (1 << BASE); i++) {
                nodes[i] = Arrays.copyOf(nodes[i], nodes(height) + capacity * (height + 1));
            }
            elements = Arrays.copyOf(elements, n + capacity);
        }

        public int set(int i, T x, int t) {
            if (i < 0 || n <= i) {
                throw new ArrayIndexOutOfBoundsException("Index " + i + " is out of bounds");
            } else if (t < 0 || lastUpdate < t) {
                throw new TimeOutOfBoundsException("Time " + t + " is out of bounds");
            }
            if (lastUpdate + 1 == roots.length) {
                growRoots(lastUpdate + 1);
            }
            if (n + newElements == elements.length) {
                growNodes(newElements + 1);
            }

            int c = roots[t];
            int d = nodes(height) + newElements * (height + 1);
            elements[n + newElements] = x;
            roots[++lastUpdate] = d;
            for (int ss = BASE * height; ss > 0; ss -= BASE) {
                for (int j = 0; j < (1 << BASE); j++) {
                    nodes[j][d] = nodes[j][c];
                }
                nodes[(i >> ss) & MASK][d] = ++d;
                c = nodes[(i >>> ss) & MASK][c];
            }

            for (int j = 0; j < (1 << BASE); j++) {
                nodes[j][d] = nodes[j][c];
            }
            nodes[i & MASK][d] = n + newElements++;
            return lastUpdate;
        }

        public int set(int i, T x) {
            return set(i, x, lastUpdate);
        }

        public int set(int t) {
            if (lastUpdate + 1 == roots.length) {
                growRoots(lastUpdate + 1);
            }
            roots[++lastUpdate] = roots[t];
            return newElements;
        }

        public static int nodes(int h) {
            return ((1 << (BASE * (h + 1))) - 1) / ((1 << BASE) - 1);
        }

    }

    static class LightWriter implements AutoCloseable {
        private final Writer out;
        private boolean autoflush = false;
        private boolean breaked = true;

        public LightWriter(Writer out) {
            this.out = out;
        }

        public LightWriter(OutputStream out) {
            this(new BufferedWriter(new OutputStreamWriter(out, Charset.defaultCharset())));
        }

        public LightWriter print(char c) {
            try {
                out.write(c);
                breaked = false;
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
            return this;
        }

        public LightWriter print(String s) {
            try {
                out.write(s, 0, s.length());
                breaked = false;
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
            return this;
        }

        public LightWriter ans(String s) {
            if (!breaked) {
                print(' ');
            }
            return print(s);
        }

        public LightWriter ans(int i) {
            return ans(Integer.toString(i));
        }

        public LightWriter ansln(int... n) {
            for (int n1 : n) {
                ans(n1).ln();
            }
            return this;
        }

        public LightWriter ln() {
            print(System.lineSeparator());
            breaked = true;
            if (autoflush) {
                try {
                    out.flush();
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
            return this;
        }

        public void close() {
            try {
                out.close();
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
        }

    }

    static class LightScanner {
        private BufferedReader reader = null;
        private StringTokenizer tokenizer = null;

        public LightScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        public String string() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int ints() {
            return Integer.parseInt(string());
        }

    }

    static class TimeOutOfBoundsException extends RuntimeException {
        public TimeOutOfBoundsException() {
        }

        public TimeOutOfBoundsException(String message) {
            super(message);
        }

        public TimeOutOfBoundsException(String message, Throwable cause) {
            super(message, cause);
        }

        public TimeOutOfBoundsException(Throwable cause) {
            super(cause);
        }

    }
}

