import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class SoftUniCoursePlanning { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List input = Arrays.stream(scanner.nextLine().split(", ")).collect(Collectors.toList()); String start = ""; int count = 1; while (!"course start".equalsIgnoreCase(start = scanner.nextLine())) { String[] startCommand = start.split(":"); if (startCommand[0].equalsIgnoreCase("Add")) { if (!input.contains(startCommand[1])) { input.add(startCommand[1]); } } else if (startCommand[0].equalsIgnoreCase("Insert")) { int index = Integer.parseInt(startCommand[2]); if (!input.contains(startCommand[1])) { input.add(index, startCommand[1]); } } else if (startCommand[0].equals("Remove")) { if (input.contains(startCommand[1])) { int index = input.indexOf(startCommand[1]); input.remove(startCommand[1]); if (input.contains(startCommand[1] + "-Exercise")) { int exerciseIndex = input.indexOf(startCommand[1] + "-Exercise"); if (index + 1 == exerciseIndex) { input.remove(startCommand[1] + "-Exercise"); } } } } else if (startCommand[0].equals("Swap")) { if (input.contains(startCommand[1]) && input.contains(startCommand[2])) { int firstIndex = input.indexOf(startCommand[1]); int secondIndex = input.indexOf(startCommand[2]); if (input.contains(startCommand[1] + "-Exercise")) { int exerciseIndex1 = input.indexOf(startCommand[1] + "-Exercise"); if (firstIndex + 1 == exerciseIndex1) { input.add(secondIndex, startCommand[1]); input.add(firstIndex+1, startCommand[2]); input.remove(firstIndex+2); input.remove(secondIndex+1); input.add(secondIndex + 1, startCommand[1] + "-Exercise"); input.remove(exerciseIndex1 + 1); } }else if (input.contains(startCommand[2] + "-Exercise")) { int secondIndexNow = input.indexOf(startCommand[2]); int exerciseIndex2 = input.indexOf(startCommand[2] + "-Exercise"); if (secondIndex + 1 == exerciseIndex2) { input.add(firstIndex, startCommand[2]); input.add(secondIndex+2, startCommand[1]); input.remove(firstIndex+1); input.remove(secondIndex); input.add(firstIndex + 1, startCommand[2] + "-Exercise"); input.remove(exerciseIndex2 + 1); } } else { input.add(firstIndex, startCommand[2]); input.set(secondIndex+1,startCommand[1]); input.remove(firstIndex+1); } } } else if (startCommand[0].equalsIgnoreCase("Exercise")) { if (input.contains(startCommand[1])) { int index = input.indexOf(startCommand[1]); input.add(index + 1, startCommand[1] + "-Exercise"); } else { input.add(startCommand[1]); input.add(startCommand[1] + "-Exercise"); } } if (input.contains(startCommand[1] + "-Exercise")) { int indexFirst = input.indexOf(startCommand[1] + "-Exercise"); if (input.contains(startCommand[1])) { int index = input.indexOf(startCommand[1]); input.add(index + 1, startCommand[1] + "-Exercise"); input.remove(indexFirst); } } } for (int i = 0; i < input.size(); i++) { System.out.println(String.join("", count + "." + input.get(i))); count++; } } }