#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<int, pair<int, string> > > crew;
  for (int i = 0; i < n; i++) {
    string name, priority;
    cin >> name >> priority;
    int p;
    if (priority == "rat")
      p = 1;
    else if (priority == "woman" || priority == "child")
      p = 3;
    else if (priority == "man")
      p = 4;
    else if (priority == "captain")
      p = 5;
    crew.push_back(make_pair(p, make_pair(i, name)));
  }
  sort(crew.begin(), crew.end());
  for (int i = 0; i < n; i++) cout << crew[i].second.second << endl;
  return 0;
}
