#include <bits/stdc++.h>
using namespace std;
const int N = 2e2 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;
const double EPS = 0.00000000001;
int n, prior;
priority_queue<pair<pair<int, int>, string>> q;
string name, rule;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    cin >> name >> rule;
    if (rule == "rat")
      prior = 4;
    else if (rule == "woman")
      prior = 3;
    else if (rule == "child")
      prior = 3;
    else if (rule == "man")
      prior = 2;
    else
      prior = 1;
    q.push({{prior, -i}, name});
  }
  while (!q.empty()) {
    cout << q.top().second << endl;
    q.pop();
  }
  return 0;
}
