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