#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  map<string, string> ship;
  string s1[100], s2[100], c[100], w[100], ch[100], r[100];
  map<string, string>::iterator itr;
  int n, k = 0, b = 0, j = 0, l = 0;
  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;
}
