import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int q = sc.nextInt(); TreeSet students = new TreeSet<>(); Map map = new HashMap<>(); List ans = new ArrayList<>(); while (q-- > 0) { int type = sc.nextInt(); if (type == 1) { String name = sc.next(); int rate = sc.nextInt(); map.put(name, new Student(name, rate)); students.add(map.get(name)); while (students.size() > n) { ans.add(students.pollLast()); } } else if (type == 2) { n -= sc.nextInt(); while (students.size() > n) { ans.add(students.pollLast()); } } else { String name = sc.next(); n += sc.nextInt(); students.remove(map.get(name)); map.get(name).fix(); students.add(map.get(name)); } } System.out.println(ans.stream().map(x -> x.toString()).collect(Collectors.joining("\n"))); } static class Student implements Comparable { String name; int value; int fixed; public Student(String name, int value) { this.name = name; this.value = value; } public void fix() { fixed = 1; } public int hashCode() { return name.hashCode(); } public boolean equals(Object o) { Student s = (Student)o; return name.equals(s.name); } public int compareTo(Student another) { return fixed == another.fixed ? another.value - value : another.fixed - fixed; } public String toString() { return name; } } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }