#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, string> &p1, const pair<int, string> &p2) {
  return p1.first < p2.first;
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, string>> names;
  while (n--) {
    string name, role;
    cin >> name >> role;
    if (role == "rat") {
      names.push_back(make_pair(1, name));
    } else if (role == "woman" || role == "child") {
      names.push_back(make_pair(2, name));
    } else if (role == "man") {
      names.push_back(make_pair(3, name));
    } else if (role == "captain") {
      names.push_back(make_pair(4, name));
    }
  }
  stable_sort(names.begin(), names.end(), cmp);
  for (int i = 0; i < names.size(); i++) cout << names[i].second << endl;
}
