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.Deque;
import java.util.ArrayDeque;
import java.util.Set;
import java.util.HashSet;
import java.util.NavigableMap;
import java.util.TreeMap;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

public class Bookcase
{
	public static void main(String[] args) throws IOException
	{
		//String str = //"4 2 6 3 2 2 2 2 3 3 3 2 2 2 2 3 2";
			//"2 3 3 1 1 1 3 2 4 0";
			//"2 2 2 3 2 2 2 1";
		//BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8"))));
		BufferedInputStream in = new BufferedInputStream(System.in);
		int n = nextInt(in), m = nextInt(in), q = nextInt(in);
		//NavigableMap<Integer, Shelf> cache = new TreeMap<>();
		Instruction[] instructions = new Instruction[q + 1];
		State[] states = new State[q + 1];
		instructions[0] = new Nop();
		State current = new State(0, 0);
		states[0] = current;
		instructions[0] = new Nop();
		for (int i = 1; i <= q; i++)
		{
			switch (nextInt(in))
			{
				case 1:
					instructions[i] = new Add(nextInt(in), nextInt(in));
					current.children.add(i);
					current = new State(i, current.k);
					states[i] = current;
					break;
				case 2:
					instructions[i] = new Remove(nextInt(in), nextInt(in));
					current.children.add(i);
					current = new State(i, current.k);
					states[i] = current;
					break;
				case 3:
					instructions[i] = new Invert(nextInt(in));
					current.children.add(i);
					current = new State(i, current.k);
					states[i] = current;
					break;
				case 4:
					//instructions[i] = new Restore(nextInt(in), i, instructions, cache);
					int k = nextInt(in);
					instructions[i] = new Restore(k, i);
					current = states[k];
					states[i] = current;
					current.reverts.add(i);
					break;
			}
		}
		Shelf shelf = new Shelf(n, m);
		Deque<State> stack = new ArrayDeque<>();
		stack.addFirst(states[0]);
		int[] counts = new int[q + 1];
		while (!stack.isEmpty())
		{
			current = stack.peekFirst();
			if (!current.visited)
			{
				//pre
				current.visited = true;
				//execute
				Instruction instr = instructions[current.k];
				current.modified = instr.execute(shelf);
				//record counts
				counts[current.k] = shelf.getCount();
				for (int i : current.reverts)
				{
					counts[i] = shelf.getCount();
				}
				//push children
				for (int c : current.children)
				{
					stack.addFirst(states[c]);
				}
			}
			else
			{
				//post
				stack.removeFirst();
				//undo if necessary
				if (current.modified)
				{
					Instruction instr = instructions[current.k];
					instr.undo(shelf);
				}
			}
		}
		//pretend to execute, accumulate counts in order
		StringBuilder sb = new StringBuilder();
		for (int k = 1; k <= q; k++)
		{
			sb.append(counts[k]);
			sb.append(System.lineSeparator());
		}
		System.out.println(sb);
		//cache.put(0, new Shelf(n, m));
		// StringBuilder sb = new StringBuilder();
		// for (int k = 1; k <= q; k++)
		// {
			// Instruction instr = instructions[k];
			// instr.execute(shelf);
			// if (k % 10000 == 0)
			// {
				// cache.put(k, shelf.copyOf());
			// }
			// sb.append(shelf.getCount());
			// sb.append(System.lineSeparator());
		// }
		// System.out.println(sb);
	}
	
	static class State 
	{
		public final int k;
		public final int p;
		public final Set<Integer> reverts;
		public final Set<Integer> children;
		public boolean visited;
		public boolean modified;
		
		public State(int k, int p)
		{
			this.k = k;
			this.p = p;
			reverts = new HashSet<Integer>();
			children = new HashSet<Integer>();
		}
	}
	
	public static int nextInt(BufferedInputStream in) throws IOException
	{
		int next = 0;
		boolean haveNext = false;
		int b;
		while ((b = in.read()) != -1)
		{
			if ('0' <= b && b <= '9')
			{
				next = 10*next + (b - '0');
				haveNext = true;
			}
			else if (haveNext)
			{
				break;
			}
		}
		return next;
	}
}

class Shelf
{
	private boolean[][] pos;
	private boolean[] flipped;
	private int[] rowCount;
	private int count = 0;
	
	public Shelf(int n, int m)
	{
		pos = new boolean[n + 1][m + 1];
		flipped = new boolean[n + 1];
		rowCount = new int[n + 1];
	}
	
	public Shelf copyOf()
	{
		Shelf newShelf = new Shelf(pos.length - 1, pos[0].length - 1);
		for (int i = 0; i < pos.length; i++)
		{
			System.arraycopy(pos[i], 0, newShelf.pos[i], 0, pos[i].length);
		}
		System.arraycopy(flipped, 0, newShelf.flipped, 0, flipped.length);
		System.arraycopy(rowCount, 0, newShelf.rowCount, 0, rowCount.length);
		newShelf.count = count;
		return newShelf;
	}
	
	public void apply(Shelf backup)
	{
		for (int i = 1; i < pos.length; i++)
		{
			System.arraycopy(backup.pos[i], 0, pos[i], 0, pos[i].length);
		}
		System.arraycopy(backup.flipped, 0, flipped, 0, flipped.length);
		System.arraycopy(backup.rowCount, 0, rowCount, 0, rowCount.length);
		count = backup.count;
	}
	
	public boolean add(int i, int j)
	{
		boolean changed = false;
		if (flipped[i])
		{
			if (pos[i][j])
			{
				rowCount[i]++;
				count++;
				changed = true;
			}
			pos[i][j] = false;
		}
		else
		{
			if (!pos[i][j])
			{
				rowCount[i]++;
				count++;
				changed = true;
			}
			pos[i][j] = true;
		}
		return changed;
	}
	
	public boolean remove(int i, int j)
	{
		boolean changed = false;
		if (flipped[i])
		{
			if (!pos[i][j])
			{
				rowCount[i]--;
				count--;
				changed = true;
			}
			pos[i][j] = true;
		}
		else
		{
			if (pos[i][j])
			{
				rowCount[i]--;
				count--;
				changed = true;
			}
			pos[i][j] = false;
		}
		return changed;
	}
	
	public boolean invert(int i)
	{
		count -= rowCount[i];
		rowCount[i] = pos[i].length - 1 - rowCount[i];
		count += rowCount[i];
		flipped[i] = !flipped[i];
		return true;
	}
	
	public int getCount()
	{
		return count;
	}
}

abstract class Instruction
{
	public abstract boolean execute(Shelf shelf);
	public abstract void undo(Shelf shelf);
}

class Nop extends Instruction
{
	public boolean execute(Shelf shelf)
	{
		return false;
	}
	public void undo(Shelf shelf)
	{
	}
}

class Add extends Instruction
{
	public final int i, j;
	public Add(int i, int j)
	{
		this.i = i;
		this.j = j;
	}
	public boolean execute(Shelf shelf)
	{
		return shelf.add(i, j);
	}
	public void undo(Shelf shelf)
	{
		shelf.remove(i, j);
	}
}

class Remove extends Instruction
{
	public final int i, j;
	public Remove(int i, int j)
	{
		this.i = i;
		this.j = j;
	}
	public boolean execute(Shelf shelf)
	{
		return shelf.remove(i, j);
	}
	public void undo(Shelf shelf)
	{
		shelf.add(i, j);
	}
}

class Invert extends Instruction
{
	public final int i;
	public Invert(int i)
	{
		this.i = i;
	}
	public boolean execute(Shelf shelf)
	{
		return shelf.invert(i);
	}
	public void undo(Shelf shelf)
	{
		shelf.invert(i);
	}
}

class Restore extends Instruction
{
	public final int k;
	public final int o;
	//private final Instruction[] instructions;
	//private final NavigableMap<Integer, Shelf> cache;
	public Restore(int k, int o)
	{
		this.k = k;
		this.o = o;
	}
	// Never executed per se
	public boolean execute(Shelf shelf)
	{
		return false;
	}
	public void undo(Shelf shelf)
	{
	}
	// public Restore(int k, int o, Instruction[] instructions, NavigableMap<Integer, Shelf> cache)
	// {
		// this.k = k;
		// this.o = o;
		// this.instructions = instructions;
		// this.cache = cache;
	// }
	// public void execute(Shelf shelf)
	// {
		// int f = cache.floorKey(k);
		// shelf.apply(cache.get(f));
		// for (int m = f + 1; m <= k; m++)
		// {
			// Instruction instr = instructions[m];
			// instr.execute(shelf);
		// }
		// if (!cache.containsKey(o))
		// {
			// Shelf shelfCopy = shelf.copyOf();
			// cache.put(k, shelfCopy);
			// cache.put(o, shelfCopy);
		// }
	// }
}