#include <bits/stdc++.h>
using namespace std;
vector<pair<string, string> > sh;
void Ship(int n) {
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < sh.size(); j++) {
      if (sh[j].second == "rat" && i == 0) cout << sh[j].first << endl;
      if (sh[j].second == "child" || sh[j].second == "woman") {
        if (i == 1) {
          cout << sh[j].first << endl;
        }
      }
      if (sh[j].second == "man" && i == 2) cout << sh[j].first << endl;
      if (sh[j].second == "captain" && i == 3) cout << sh[j].first << endl;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  string x, y;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    sh.push_back(make_pair(x, y));
  }
  Ship(n);
  return 0;
}
