#include <bits/stdc++.h>
using namespace std;
const int MAXSIZE = 1000 + 10;
int main() {
  vector<string> cap, cw, rat, man;
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    if (s2 == "captain")
      cap.push_back(s1);
    else if (s2 == "woman" || s2 == "child")
      cw.push_back(s1);
    else if (s2 == "rat")
      rat.push_back(s1);
    else
      man.push_back(s1);
  }
  for (int i = 0; i < (int)rat.size(); i++) cout << rat[i] << '\n';
  for (int i = 0; i < (int)cw.size(); i++) cout << cw[i] << '\n';
  for (int i = 0; i < (int)man.size(); i++) cout << man[i] << '\n';
  for (int i = 0; i < (int)cap.size(); i++) cout << cap[i] << '\n';
  return 0;
}
