#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string a, b;
  vector<pair<string, string> > A;
  while (n--) {
    cin >> a >> b;
    if (b == "rat")
      cout << a << "\n";
    else
      A.push_back(make_pair(a, b));
  }
  n = 1;
  while (n != 4) {
    for (int i = 0; i < A.size(); i++) {
      if (n == 1 and (A[i].second == "woman" or A[i].second == "child"))
        cout << A[i].first << "\n";
      else if (n == 2 and A[i].second == "man")
        cout << A[i].first << "\n";
      else if (n == 3 and A[i].second == "captain")
        cout << A[i].first << "\n";
    }
    n++;
  }
  return 0;
}
