#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>
using namespace std;
int main() {
int n, m; //人数和科目数量
while (cin >> n >> m) {
vector<string> subjects(m);
//科目录入
for (int i = 0; i < m; i++) {
cin >> subjects[i];
}
cin.ignore();
vector<pair<string,vector<int>>> students;
for (int i = 0; i < n; i++) { //读取每个孩子的分数
string s;
getline(cin, s);
istringstream iss(s);
string name;
iss >> name;
int score;
vector<int> scores;
int sum = 0;//计算总得分,放在分数数组最后
while (iss >> score) {
sum += score;
scores.push_back((int)score);
}
scores.push_back(sum);//最后录入总分
students.push_back(make_pair(name, scores));
}
string res;
cin >> res;
//查找排序基准
int index = m;//初始为总分index
for (int i = 0; i < m; i++) {
if (subjects[i] == res) {
index = i;
break;
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n-i-1;j++){
if(students[j].second[index]<students[j+1].second[index]||
(students[j].second[index]==students[j+1].second[index]&&students[j].first > students[j+1].first)){
//成绩相同,字典小的在前
swap(students[j],students[j+1]);
}
}
}
for(auto it = students.begin();it!=students.end();it++){
cout << it->first <<' ';
}
cout << endl;
}
}