import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);//新创建一个输入的Scanner 对象,然后赋值给in
int n = scan.nextInt();
scan.nextLine();
String A[] = new String[n], C[] = new String[n];
for (int i = 0; i < n; i++) {
A[i] = scan.nextLine();
}
String B = scan.nextLine();//查找并返回下一个完整标记从这个扫描仪
for (; B.equals("sort1") || B.equals("sort2");) {
if (B.equals("sort1")) {
for (int j = 0; j < n; j++) {
C[j] = A[j].substring(6, 10) + "-" + A[j].substring(10, 12) + "-" + A[j].substring(12, 14);//返回第一个参数中从第二个参数指定的位置开始、第三个参数指定的长度的子字符串
}
Arrays.sort(C);
for (int j = 0; j < n; j++) {
System.out.println(C[j].toString());
}
} else if (B.equals("sort2")) {
for (int j = 0; j < n; j++) {
C[j] = A[j].substring(6, 14);
}
Arrays.sort(C);//Arrays中的sort()方法主要是针对各种数据类型(基本数据类型和引用对象类型)的数组元素排序
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
if (A[k].contains(C[j]))
System.out.println(A[k].toString());
}
System.out.println("exit");
B = scan.nextLine();
}
scan.close();
}
}