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