#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string rat[100], cw[100], man[100], c;
  int a = 0, b = 0, d = 0;
  for (int i = 0; i < n; i++) {
    string name, s;
    cin >> name >> s;
    if (s == "captain")
      c = name;
    else if (s == "woman" || s == "child") {
      cw[a] = name;
      a++;
    } else if (s == "rat") {
      rat[b] = name;
      b++;
    } else if (s == "man") {
      man[d] = name;
      d++;
    } else if (s == "captain")
      c = name;
  }
  for (int i = 0; i <= b; i++) {
    if (i == b)
      cout << rat[i];
    else
      cout << rat[i] << endl;
  }
  for (int i = 0; i <= a; i++) {
    if (i == a)
      cout << cw[i];
    else
      cout << cw[i] << endl;
  }
  for (int i = 0; i <= d; i++) {
    if (i == d)
      cout << man[i];
    else
      cout << man[i] << endl;
  }
  cout << c << endl;
}
