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