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