// Arup Guha
// 2/19/2026
// Code to double check my C map example.

import java.util.*;

public class mapsol {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Create my map.
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		
		// Process operations.
		for (int i=0; i<n; i++) {
		
			// Read it.
			int op = stdin.nextInt();
			String s = stdin.next();
			
			// Add 1
			if (op == 1) {
				if (!map.containsKey(s)) map.put(s, 1);
				else map.put(s, map.get(s)+1);
			}
			
			// Remove 1
			else if (op == 2) {
				if (!map.containsKey(s)) continue;
				if (map.get(s) == 1)
					map.remove(s);
				else
					map.put(s, map.get(s)-1);
			}
			
			// Query
			else if (op == 3) {
				if (!map.containsKey(s))
					System.out.println("0");
				else
					System.out.println(map.get(s));
			}
			
			// Remove the entry.
			else {
				if (!map.containsKey(s)) continue;
				map.remove(s);
			}
		}
	}
}