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.util.*;
public final class persistant_bookcase
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static Random rnd=new Random();
	static int[] tree,lazy,res;
	static ArrayList<Pair>[] al;
	static ArrayList<Integer>[] al2;
	static Query[] qr;
	static int n,m,q;
	
	static boolean perform(int i)
	{
		if(qr[i].type==1)
		{
			int s=(qr[i].i-1)*m;
			if(query2(1,0,(n*m)-1,s+qr[i].j-1,s+qr[i].j-1)==0)
			{
				update2(1,0,(n*m)-1,s+qr[i].j-1,s+qr[i].j-1,1);
				return true;
			}
			return false;
		}
		else if(qr[i].type==2)
		{
			int s=(qr[i].i-1)*m;
			if(query2(1,0,(n*m)-1,s+qr[i].j-1,s+qr[i].j-1)==1)
			{
				update2(1,0,(n*m)-1,s+qr[i].j-1,s+qr[i].j-1,-1);
				return true;
			}
			return false;
		}
		else
		{
			int s=(qr[i].i-1)*m,e=(qr[i].i*m)-1;
			update1(1,0,(n*m)-1,s,e,1);
			return true;
		}
	}
	
	static void deperform(int i)
	{
		if(qr[i].type==1)
		{
			int s=(qr[i].i-1)*m;		
			update2(1,0,(n*m)-1,s+qr[i].j-1,s+qr[i].j-1,-1);
		}
		else if(qr[i].type==2)
		{
			int s=(qr[i].i-1)*m;
			update2(1,0,(n*m)-1,s+qr[i].j-1,s+qr[i].j-1,1);
		}
		else
		{
			int s=(qr[i].i-1)*m,e=(qr[i].i*m)-1;
			update1(1,0,(n*m)-1,s,e,1);
		}
	}
	
	static void dfs(int u)
	{	
		for(int x:al2[u])
		{
			res[x]=query1(1,0,(n*m)-1,0,(n*m)-1);
		}
		for(Pair p:al[u])
		{
			boolean curr=perform(p.a);
			dfs(p.b);
			if(curr)
			{
				deperform(p.a);
			}
		}
	}
	
	static void update1(int node,int s,int e,int l,int r,int val)
	{
		if(lazy[node]!=0)
		{
			int curr=lazy[node]%2;
			if(curr==1)
			{
				tree[node]=(e-s+1)-tree[node];
				if(s!=e)
				{
					lazy[node<<1]+=lazy[node];
					lazy[node<<1|1]+=lazy[node];
				}
			}
			lazy[node]=0;
		}
		if(s>e || l>e || r<s)
		{
			return;
		}
		if(l<=s && r>=e)
		{
			tree[node]=(e-s+1)-tree[node];
			if(s!=e)
			{
				lazy[node<<1]+=val;lazy[node<<1|1]+=val;
			}
		}
		else
		{
			int mid=(s+e)>>1;
			update1(node<<1,s,mid,l,r,val);update1(node<<1|1,mid+1,e,l,r,val);
			tree[node]=tree[node<<1]+tree[node<<1|1];
		}
	}
	
	static void update2(int node,int s,int e,int l,int r,int val)
	{
		if(lazy[node]!=0)
		{
			int curr=lazy[node]%2;
			if(curr==1)
			{
				tree[node]=(e-s+1)-tree[node];
				if(s!=e)
				{
					lazy[node<<1]+=lazy[node];
					lazy[node<<1|1]+=lazy[node];
				}
			}
			lazy[node]=0;
		}
		if(s>e || l>e || r<s)
		{
			return;
		}
		if(s==e)
		{
			tree[node]+=val;
		}
		else
		{
			int mid=(s+e)>>1;
			update2(node<<1,s,mid,l,r,val);update2(node<<1|1,mid+1,e,l,r,val);
			tree[node]=tree[node<<1]+tree[node<<1|1];
		}
	}
	
	static int query1(int node,int s,int e,int l,int r)
	{
		if(lazy[node]!=0)
		{
			int curr=lazy[node]%2;
			if(curr==1)
			{
				tree[node]=(e-s+1)-tree[node];
				if(s!=e)
				{
					lazy[node<<1]+=lazy[node];
					lazy[node<<1|1]+=lazy[node];
				}
			}
			lazy[node]=0;
		}
		if(s>e || l>e || r<s)
		{
			return 0;
		}
		if(l<=s && r>=e)
		{
			return tree[node];
		}
		else
		{
			int mid=(s+e)>>1;
			int val1=query1(node<<1,s,mid,l,r),val2=query1(node<<1|1,mid+1,e,l,r);
			return (val1+val2);
		}
	}
	
	static int query2(int node,int s,int e,int l,int r)
	{
		if(lazy[node]!=0)
		{
			int curr=lazy[node]%2;
			if(curr==1)
			{
				tree[node]=(e-s+1)-tree[node];
				if(s!=e)
				{
					lazy[node<<1]+=lazy[node];
					lazy[node<<1|1]+=lazy[node];
				}
			}
			lazy[node]=0;
		}
		if(s>e || l>e || r<s)
		{
			return 0;
		}
		if(s==e)
		{
			return tree[node];
		}
		else
		{
			int mid=(s+e)>>1;
			int val1=query2(node<<1,s,mid,l,r),val2=query2(node<<1|1,mid+1,e,l,r);
			return (val1+val2);
		}
	}
	
	@SuppressWarnings("unchecked")
    public static void main(String args[]) throws Exception
    {
		n=sc.nextInt();m=sc.nextInt();q=sc.nextInt();qr=new Query[q+1];res=new int[q+1];
		for(int i=1;i<=q;i++)
		{
			int t=sc.nextInt();
			if(t==1 || t==2)
			{
				qr[i]=new Query(t,sc.nextInt(),sc.nextInt());
			}
			else
			{
				qr[i]=new Query(t,sc.nextInt(),-1);
			}
		}
		al=new ArrayList[q+1];al2=new ArrayList[q+1];
		for(int i=0;i<=q;i++)
		{
			al[i]=new ArrayList<Pair>();
			al2[i]=new ArrayList<Integer>();
		}
		int v=0,ctr=0;int[] version=new int[q+1];
		for(int i=1;i<=q;i++)
		{
			if(qr[i].type==4)
			{
				version[i]=version[qr[i].i];v=version[i];
			}
			else
			{
				version[i]=++ctr;al[v].add(new Pair(i,ctr));v=version[i];
			}
		}
		for(int i=1;i<=q;i++)
		{
			al2[version[i]].add(i);
		}
		tree=new int[4*n*m+5];lazy=new int[4*n*m+5];dfs(0);
		for(int i=1;i<=q;i++)
		{
			out.println(res[i]);
		}
		out.close();
    }
}
class Pair
{
	int a,b;
	public Pair(int a,int b)
	{
		this.a=a;this.b=b;
	}
}
class Query
{
	int type,i,j;
	public Query(int type,int i,int j)
	{
		this.type=type;this.i=i;this.j=j;
	}
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}