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