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