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