#include <bits/stdc++.h>
using namespace std;
int main() {
  string str, type, r[101], w[101], m[101], c[101];
  int n, cr = 0, cw = 0, cm = 0, cc = 0;
  cin >> n;
  while (n--) {
    cin >> str >> type;
    if (type == "rat")
      r[cr++] = str;
    else if (type == "woman" || type == "child")
      w[cw++] = str;
    else if (type == "man")
      m[cm++] = str;
    else
      c[cc++] = str;
  }
  if (cr > 0)
    for (int i = 0; i < cr; i++) cout << r[i] << endl;
  if (cw > 0)
    for (int i = 0; i < cw; i++) cout << w[i] << endl;
  if (cm > 0)
    for (int i = 0; i < cm; i++) cout << m[i] << endl;
  if (cc > 0)
    for (int i = 0; i < cc; i++) cout << c[i] << endl;
  return 0;
}
