#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string s1[101], s2[101];
  string ss1, ss2;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> ss1 >> ss2;
    s1[i] = ss1;
    s2[i] = ss2;
  }
  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;
}
