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