#include <bits/stdc++.h>
using namespace std;
int main() {
  map<string, int> t;
  t["captain"] = 3;
  t["man"] = 2;
  t["child"] = 1;
  t["woman"] = 1;
  t["rat"] = 0;
  int n;
  scanf("%d", &n);
  vector<pair<pair<int, int>, string> > v;
  for (int i = 0; i < n; i++) {
    string a, b;
    cin >> a >> b;
    v.push_back(make_pair(make_pair(t[b], i), a));
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) cout << v[i].second << '\n';
}
