#include <bits/stdc++.h>
using namespace std;
bool Comp(const pair<int, string>& a, const pair<int, string>& b) {
  return a.first < b.first;
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, string> > res(n);
  for (int i = 0; i < n; i++) {
    pair<int, string> tmp;
    string type;
    cin >> tmp.second >> type;
    if (type == "rat") tmp.first = 0;
    if (type == "woman") tmp.first = 1;
    if (type == "child") tmp.first = 1;
    if (type == "man") tmp.first = 2;
    if (type == "captain") tmp.first = 3;
    res[i] = tmp;
  }
  stable_sort(res.begin(), res.end(), Comp);
  for (int i = 0; i < res.size(); i++) cout << res[i].second << endl;
  return 0;
}
