#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<string> rat;
  vector<string> woman;
  vector<string> man;
  vector<string> captain;
  for (int i = 0; i < n; i++) {
    string x, y;
    cin >> x >> y;
    if (y == "rat")
      rat.push_back(x);
    else if (y == "woman" || y == "child")
      woman.push_back(x);
    else if (y == "man")
      man.push_back(x);
    else
      captain.push_back(x);
  }
  for (auto it : rat) cout << it << "\n";
  for (auto it : woman) cout << it << "\n";
  for (auto it : man) cout << it << "\n";
  for (auto it : captain) cout << it << "\n";
  return 0;
}
