#include <bits/stdc++.h>
using namespace std;
int make(string s, bool noZeros) {
  int n = s.size();
  int ten = 1, ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0' && noZeros) continue;
    ans += (s[i] - '0') * ten;
    ten *= 10;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  vector<string> men, women, rat;
  string s, rank, captain;
  for (int i = 0; i < n; i++) {
    cin >> s >> rank;
    if (rank == "man")
      men.push_back(s);
    else if (rank == "rat")
      rat.push_back(s);
    else if (rank == "woman" || rank == "child")
      women.push_back(s);
    else
      captain = s;
  }
  for (int i = 0; i < rat.size(); i++) cout << rat[i] << endl;
  for (int i = 0; i < women.size(); i++) cout << women[i] << endl;
  for (int i = 0; i < men.size(); i++) cout << men[i] << endl;
  cout << captain << endl;
  return 0;
}
