#include
#include
#include
#include
using namespace std;
string extractStringatKey(string str,int key){
char *arr = strtok((char *)str.c_str()," “);
while(key > 1){
arr = strtok(NULL,” ");
key–;
}
return (string)arr;
}
bool numericSort(pair<string,string> str1,pair<string,string> str2){
string key1;
string key2;
key1 = str1.second;
key2 = str2.second;
return stoi(key1) < stoi(key2);
}
bool lexicoSort(pair<string,string> str1,pair<string,string> str2){
string key1;
string key2;
key1 = str1.second;
key2 = str2.second;
return key1 < key2;
}
int main(){
int n;
cin >>n;
cin.get();
string s[100];
for(int i = 0;i<n;i++){
getline(cin,s[i]);
}
int key;
string reversed;
string sortType;
cin >> key >> reversed >> sortType;
pair<string,string> strPair[100];
for(int i = 0 ;i < n ; i++){
strPair[i].first = s[i];
strPair[i].second = extractStringatKey(s[i],key);
}
if(sortType == "numeric"){
sort(strPair,strPair+n,numericSort);
}
else if(sortType == "lexicographical"){
sort(strPair,strPair+n,lexicoSort);
}
if(reversed == "true"){
for(int i = 0;i<n/2;i++){
swap(strPair[i],strPair[n-i-1]);
}
}
for(int i = 0;i<n;i++){
cout<<strPair[i].first<<endl;
}
return 0;
}