#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, w = 0, r = 0, m = 0;
  cin >> n;
  string s, t;
  vector<string> wc, rt, man, cpt;
  for (i = 0; i < n; i++) {
    cin >> s >> t;
    if (t == "rat")
      rt.push_back(s), r++;
    else if (t == "woman" || t == "child")
      wc.push_back(s), w++;
    else if (t == "man")
      man.push_back(s), m++;
    else
      cpt.push_back(s);
  }
  for (i = 0; i < r; i++) cout << rt[i] << '\n';
  for (i = 0; i < w; i++) cout << wc[i] << '\n';
  for (i = 0; i < m; i++) cout << man[i] << '\n';
  cout << cpt[0];
}
