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