#include <bits/stdc++.h>
using namespace std;
struct node {
  string name;
  int ci;
  int id;
  friend bool operator<(node a, node b) {
    if (a.ci == b.ci) return a.id > b.id;
    return a.ci > b.ci;
  }
} str[105];
int main() {
  int n;
  cin >> n;
  string s;
  priority_queue<node> q;
  for (int i = 1; i <= n; i++) {
    cin >> str[i].name >> s;
    if (s == "rat")
      str[i].ci = 1;
    else if (s == "child")
      str[i].ci = 2;
    else if (s == "woman")
      str[i].ci = 2;
    else if (s == "man")
      str[i].ci = 3;
    else
      str[i].ci = 4;
    str[i].id = i;
    q.push(str[i]);
  }
  while (n--) {
    cout << q.top().name << endl;
    q.pop();
  }
}
