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