#include <bits/stdc++.h>
using namespace std;
bool compare(pair<pair<int, int>, string> i1, pair<pair<int, int>, string> i2) {
  if (i1.first.first == i2.first.first) {
    return (i1.first.second < i2.first.second);
  }
  return (i1.first.first < i2.first.first);
}
int main() {
  int i, n, pos, len;
  cin >> n;
  string name[n], temp, rname;
  getline(cin, temp);
  string setup[5] = {"rat", "woman", "man", "captain", "child"};
  pair<pair<int, int>, string> a[n];
  for (i = 0; i < n; i++) getline(cin, name[i]);
  for (i = 0; i < n; i++) {
    int j = 0;
    len = name[i].length();
    pos = name[i].find(" ");
    rname = name[i].substr(0, pos);
    temp = name[i].substr(pos + 1, len - 1);
    while (temp != setup[j]) j++;
    if (j == 4) j = 1;
    a[i].first.first = j;
    a[i].first.second = i;
    a[i].second = rname;
  }
  sort(a, a + n, compare);
  for (i = 0; i < n; i++) cout << a[i].second << '\n';
  return 0;
}
