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