#include <bits/stdc++.h>
using namespace std;
struct my {
  string s;
  long long x, y;
};
vector<my> v;
bool cmp(my a, my b) {
  if (a.x != b.x)
    return a.x < b.x;
  else {
    return a.y < b.y;
  }
}
int main() {
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    my p;
    string s1, s2;
    cin >> s1 >> s2;
    p.s = s1;
    p.y = i;
    if (s2 == "captain") p.x = 4;
    if (s2 == "man") p.x = 3;
    if (s2 == "child") p.x = 2;
    if (s2 == "woman") p.x = 2;
    if (s2 == "rat") p.x = 1;
    v.push_back(p);
  }
  sort(v.begin(), v.end(), cmp);
  for (int i = 0; i < v.size(); i++) {
    cout << v[i].s << endl;
  }
}
