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.*;
import java.math.*;
import java.util.*;

import static java.util.Arrays.*;

public class D2 {
	private static final int mod = (int)1e9+7;

	final Random random = new Random(0);
	final IOFast io = new IOFast();

	/// MAIN CODE
	public void run() throws IOException {
//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
		int TEST_CASE = 1;
		while(TEST_CASE-- != 0) {
			int n = io.nextInt();
			int m = io.nextInt();
			int q = io.nextInt();
			Seg[] seg = new Seg[q+1];
			seg[0] = new Seg(n*m);
			for(int i = 0; i < q; i++) {
				int t = io.nextInt();
				if(t == 1) {
					int a = io.nextInt() - 1;
					int b = io.nextInt() - 1;
					seg[i+1] = seg[i].set(a*m + b, 1);
					io.out.println(seg[i+1].get(0, n*m));
				}
				if(t == 2) {
					int a = io.nextInt() - 1;
					int b = io.nextInt() - 1;
					seg[i+1] = seg[i].set(a*m + b, 0);
					io.out.println(seg[i+1].get(0, n*m));
				}
				if(t == 3) {
					int a = io.nextInt() - 1;
					seg[i+1] = seg[i].inv(a*m, (a+1)*m);
					io.out.println(seg[i+1].get(0, n*m));
				}
				if(t == 4) {
					int a = io.nextInt();
					seg[i+1] = seg[a];
					io.out.println(seg[i+1].get(0, n*m));
				}
			}
		}
	}

	static
	class Seg {
		final int n;
		SegNode root;
		
		public Seg(int n) {
			if(n <= 0) n = 1;
			if(Integer.bitCount(n) != 1) {
				n = Integer.highestOneBit(n) << 1;
			}
			this.n = n;
			root = SegNode.create(n);
		}
		
		public Seg(int n, SegNode root) {
			this.n = n;
			this.root = root;
		}
		
		int get(int l, int r) { return root.get(l, r, 0, n); }
		Seg inv(int l, int r) { return new Seg(n, root.inv(l, r, 0, n)); }
		Seg set(int l, int v) { return new Seg(n, root.set(l, v, 0, n)); }
	}
	
	static
	class SegNode {
		SegNode l, r;
		final int seg;
		boolean inv;
		
		public static SegNode create(int n) {
			if(n == 0) return null;
			SegNode s = new SegNode(n);
			s.l = create(n / 2);
			s.r = create(n / 2);
			return s;
		}

		public SegNode(final int n) {
			this.seg = 0;
		}

		public SegNode(SegNode l, SegNode r, int v, boolean inv) {
			this.l = l;
			this.r = r;
			this.seg = v;
			this.inv = inv;
		}
		
		void propagate(int curL, int curR) {
			if(inv) {
				final int curM = (curL + curR) / 2;
				l = l.inv(curL, curR, curL, curM);
				r = r.inv(curL, curR, curM, curR);
				inv = false;
			}
		}

		int get(int l, int r, int curL, int curR) {
			if(curR <= l || curL >= r) return 0;
			if(l <= curL && curR <= r) { return seg; }
			propagate(curL, curR);
			final int curM = (curL + curR) / 2;
			return 
					this.l.get(l, r, curL, curM) +
					this.r.get(l, r, curM, curR);
		}

		SegNode inv(int l, int r, int curL, int curR) {
			if(curR <= l || curL >= r) return this;
			if(l <= curL && curR <= r) {
//				System.err.println(curL + " " + curR + " " + n.seg);
				return new SegNode(this.l, this.r, curR - curL - seg, !inv);
			}
			final int curM = (curL + curR) / 2;
			propagate(curL, curR);
			SegNode ln = this.l.inv(l, r, curL, curM);
			SegNode rn = this.r.inv(l, r, curM, curR);
			int seg = ln.seg + rn.seg;
			return new SegNode(ln, rn, seg, false);
		}

		SegNode set(int l, int v, int curL, int curR) {
			final int r = l + 1;
			if(curR <= l || curL >= r) return this;
			if(l <= curL && curR <= r) {
				return new SegNode(this.l, this.r, v, false);
			}
			final int curM = (curL + curR) / 2;
//			System.err.println(l + " " + v + " " + curL + " " + curR + " " + n.xor);
			propagate(curL, curR);
			SegNode ln = this.l.set(l, v, curL, curM);
			SegNode rn = this.r.set(l, v, curM, curR);
			int seg = ln.seg + rn.seg;
			return new SegNode(ln, rn, seg, false);
		}
	}

	/// TEMPLATE
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }
	
	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }

	void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	
	static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } 
	
	void main() throws IOException {
		//		IOFast.setFileIO("rle-size.in", "rle-size.out");
		try { run(); }
		catch (EndOfFileRuntimeException e) { }
		io.out.flush();
	}
	public static void main(String[] args) throws IOException { new D2().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

	static
	public class IOFast {
		private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		private PrintWriter out = new PrintWriter(System.out);

		void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); }
		void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); }
		void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); }

		private static int pos, readLen;
		private static final char[] buffer = new char[1024 * 8];
		private static char[] str = new char[500*8*2];
		private static boolean[] isDigit = new boolean[256];
		private static boolean[] isSpace = new boolean[256];
		private static boolean[] isLineSep = new boolean[256];

		static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }
		public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; }
		public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }
		int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }
		public String nextString() throws IOException { return new String(next()); }
		public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }
//		public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }
		public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }
		public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }
		public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; }
		public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; }
		public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }
		public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; }
	}
}
