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