#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s, type;
  cin.ignore();
  vector<string> w_c;
  vector<string> m;
  string cap;
  while (n--) {
    cin >> s >> type;
    if (type == "rat") cout << s << endl;
    if (type == "woman" || type == "child")
      w_c.push_back(s);
    else if (type == "man")
      m.push_back(s);
    else if (type == "captain")
      cap = s;
  }
  for (int i = 0; i < w_c.size(); i++) cout << w_c[i] << endl;
  for (int i = 0; i < m.size(); i++) cout << m[i] << endl;
  cout << cap << endl;
  return 0;
}
