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