#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s, h;
  vector<pair<string, string> > t1, t2, t3, t4;
  for (int i = 0; i < n; i++) {
    cin >> s >> h;
    if (h == "rat")
      t1.push_back({s, h});
    else if (h == "woman" || h == "child")
      t2.push_back({s, h});
    else if (h == "man")
      t3.push_back({s, h});
    else
      t4.push_back({s, h});
  }
  for (int i = 0; i < t1.size(); i++) cout << t1[i].first << endl;
  for (int i = 0; i < t2.size(); i++) cout << t2[i].first << endl;
  for (int i = 0; i < t3.size(); i++) cout << t3[i].first << endl;
  for (int i = 0; i < t4.size(); i++) cout << t4[i].first << endl;
  return 0;
}
