#include <bits/stdc++.h>
using namespace std;
vector<int> G[100009];
map<string, int> pr;
struct comp {
  bool operator()(const pair<string, int>& l, const pair<string, int>& r) {
    return l.second > r.second;
  }
};
int main() {
  pr["captain"] = 1;
  pr["man"] = 2;
  pr["child"] = 3;
  pr["woman"] = 3;
  pr["rat"] = 4;
  int n;
  cin >> n;
  getchar();
  vector<pair<string, int> > v;
  while (n--) {
    string a, b;
    cin >> a >> b;
    v.push_back({a, pr[b]});
  }
  stable_sort(v.begin(), v.end(), comp());
  for (int i = 0; i < v.size(); i++) cout << v[i].first << endl;
  return 0;
}
