#include <bits/stdc++.h>
using namespace std;
struct name {
  string names;
  string desig;
};
int main() {
  int n, i = 0;
  cin >> n;
  name people[n];
  string captain;
  while (i < n) {
    cin >> people[i].names >> people[i].desig;
    i++;
  }
  for (int i = 0; i < n; i++) {
    if (people[i].desig == "captain") captain = people[i].names;
    if (people[i].desig == "rat") cout << people[i].names << endl;
  }
  for (int i = 0; i < n; i++) {
    if (people[i].desig == "woman" || people[i].desig == "child")
      cout << people[i].names << endl;
  }
  for (int i = 0; i < n; i++) {
    if (people[i].desig == "man") cout << people[i].names << endl;
  }
  cout << captain;
  return 0;
}
