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

import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int q = sc.nextInt();
		DS[] ds = new DS[q + 1];
		ds[0] = new DS(n, m);
		for (int i = 1; i <= q; i++) {
			int mode = sc.nextInt();
			if (mode == 4) {
				ds[i] = ds[sc.nextInt()];
			} else if (mode == 3) {
				ds[i] = ds[i - 1].op(mode, sc.nextInt() - 1);
			} else {
				ds[i] = ds[i - 1].op(mode, sc.nextInt() - 1, sc.nextInt() - 1);
			}
			pw.println(ds[i].query());
		}
//		for (int i = 1; i < q; i++) {
//			ds[i].print();
//			System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
//		}
		pw.close();
	}

	static class DS {
		Node books;
		Node inverted;
		int n, m;
		int sum;

		public DS(int n, int m) {
			books = build(0, n * m - 1);
			inverted = build(0, n - 1);
			this.n = n;
			this.m = m;
		}

		public DS(Node books, Node inverted, int n, int m, int sum) {
			this.books = books;
			this.inverted = inverted;
			this.n = n;
			this.m = m;
			this.sum = sum;
		}

		public static Node build(int lo, int hi) {
			if (lo >= hi) {
				return new Node();
			}
			int mid = lo + hi >> 1;
			return new Node(build(lo, mid), build(mid + 1, hi));
		}

		public DS op(int mode, Integer... param) {

			if (mode == 1) {
				return set(param[0], param[1], 1);
			} else if (mode == 2) {
				return set(param[0], param[1], 0);
			} else {
				return invert(param[0]);
			}

		}

		public int query() {
			return sum;
		}

		public int isInverted(int i) {
			return isOn(i, 0, n - 1, inverted);
		}

		public int isOn(int i) {
			return isOn(i, 0, n * m - 1, books);
		}

		public int isOn(int i, int lo, int hi, Node cur) {
			int mid = lo + hi >> 1;
			if (lo == hi) {
				return cur.cnt;
			}
			if (i <= mid) {
				return isOn(i, lo, mid, cur.l);
			} else {
				return isOn(i, mid + 1, hi, cur.r);
			}
		}

		public int query(int i, int j, int lo, int hi, Node cur) {
			int mid = lo + hi >> 1;
			if (lo > j || hi < i) {
				return 0;
			}
			if (i <= lo && hi <= j) {
				return cur.cnt;
			}

			return query(i, j, lo, mid, cur.l) + query(i, j, mid + 1, hi, cur.r);
		}

		public DS invert(int i) {
			Node newinverted = invert(i, inverted, 0, n - 1);
			int isInverted = isInverted(i);
			int ones = query(i * m, (i + 1) * m - 1, 0, n * m - 1, books);
			int zeros = m - ones;
//			System.out.println(zeros + " " + ones);
			int newsum = sum;
			if (isInverted == 0) {
				newsum += -ones + zeros;
			} else {
				newsum += ones - zeros;
			}
			return new DS(books, newinverted, n, m, newsum);

		}

		public Node invert(int i, Node cur, int lo, int hi) {
			if (lo == hi && lo == i) {
//				System.out.println(lo);
				return new Node(1 ^ cur.cnt);
			}
			int mid = lo + hi >> 1;
			if (lo <= i && i <= hi) {
				return new Node(invert(i, cur.l, lo, mid), invert(i, cur.r, mid + 1, hi));
			} else {
				return cur;
			}
		}

		public DS set(int i, int j, int val) {
			int idx = i * m + j;
			int bit = isOn(idx);
			int isInverted = isInverted(i);

			Node newbooks;
			int newsum = sum;
			if (isInverted == 0) {
				newsum += -bit + val;
				newbooks = set(idx, val, books, 0, n * m - 1);
			} else {
				newsum += -(1 ^ bit) + val;
				newbooks = set(idx, val ^ 1, books, 0, n * m - 1);
			}
			return new DS(newbooks, inverted, n, m, newsum);
		}

		public Node set(int i, int val, Node cur, int lo, int hi) {
			if (lo == hi && i == lo) {
				return new Node(val);
			}
			int mid = lo + hi >> 1;
			if (lo <= i && i <= hi) {
				return new Node(set(i, val, cur.l, lo, mid), set(i, val, cur.r, mid + 1, hi));
			} else {
				return cur;
			}
		}

		public void print() {
//			print2DUtil(books, 5, 0, n * m - 1);
			System.out.println("---------------------------");
			print2DUtil(inverted, 5, 0, n - 1);
		}

	}

	static void print2DUtil(Node root, int space, int lo, int hi) {
		// Base case
		if (lo > hi)
			return;
		if (lo == hi) {
			System.out.print("\n");
			for (int i = 10; i < space; i++)
				System.out.print(" ");
			System.out.print("( " + lo + " " + hi + " " + root.cnt + " )\n");
			return;
		}

		// Increase distance between levels
		space += 10;
		int mid = lo + hi >> 1;
		// Process right child first
		print2DUtil(root.r, space, mid + 1, hi);

		// Print current node after space
		// count
		System.out.print("\n");
		for (int i = 10; i < space; i++)
			System.out.print(" ");
		System.out.print("( " + lo + " " + hi + " " + root.cnt + " )\n");

		// Process left child
		print2DUtil(root.l, space, lo, mid);
	}

	static class Node {
		Node l, r;
		int cnt;

		public Node() {

		}

		public Node(int cnt) {
			this.cnt = cnt;
		}

		public Node(Node l, Node r) {
			this.l = l;
			this.r = r;
			this.cnt = l.cnt + r.cnt;
		}
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}

	 													  			 	 	  		