#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<pair<string, int> > c;
  int n;
  cin >> n;
  pair<string, int> m;
  int rc = 0, wc = 0, mc = 0, cc = 0;
  while (n--) {
    cin >> m.first;
    string p;
    cin >> p;
    if (p == "rat")
      m.second = 0 + rc++;
    else if (p == "woman" || p == "child")
      m.second = 100 + wc++;
    else if (p == "man")
      m.second = 200 + mc++;
    else
      m.second = 300 + cc++;
    c.push_back(m);
  }
  for (vector<pair<string, int> >::iterator it = c.begin(); it < c.end() - 1;
       it++) {
    vector<pair<string, int> >::iterator m = it;
    for (vector<pair<string, int> >::iterator it1 = it + 1; it1 < c.end();
         it1++) {
      if (it1->second < m->second) m = it1;
    }
    iter_swap(m, it);
  }
  for (vector<pair<string, int> >::iterator it = c.begin(); it != c.end(); it++)
    cout << it->first << endl;
  return 0;
}
