// Arup Guha
// 10/28/2026
// Solution to COP 3330 Fall 2026 Program 7A: Strange Line for Tickets

import java.util.*;

public class StrangeLine {
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int numOps = stdin.nextInt();
		
		// Empty line.
		ArrayList<Person> line = new ArrayList<Person>();
		
		// Process operations.
		for (int loop=0; loop<numOps; loop++) {
			
			// Get operation.
			int op = stdin.nextInt();
			
			// Add someone to the line.
			if (op == 1 || op == 2) {
				
				// Read and create object.
				String first = stdin.next();
				String last = stdin.next();
				int numTix = stdin.nextInt();
				Person tmp = new Person(first, last, numTix);
				
				// Read position and add.
				if (op == 2) {
					int idx = stdin.nextInt();
					line.add(idx, tmp);
				}
				
				// Just the back.
				else 
					line.add(tmp);
			}
			
			// Remove from the line 
			else {
				
				// Default removal position.
				int idx = 0;
				
				// Only in option 4 do we read in the position to remove.
				if (op == 4)
					idx = stdin.nextInt();
			
				// Ta da!
				System.out.println(line.remove(idx));
			}
		}
	}

}