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