#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  pair<string, string> p[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < n; j++) {
      if (i == 0) {
        if (p[j].second == "rat") {
          cout << p[j].first << '\n';
        }
      } else if (i == 1) {
        if (p[j].second == "woman" || p[j].second == "child") {
          cout << p[j].first << '\n';
        }
      } else if (i == 2) {
        if (p[j].second == "man") {
          cout << p[j].first << '\n';
        }
      } else {
        if (p[j].second == "captain") {
          cout << p[j].first << '\n';
        }
      }
    }
  }
}
