#include <bits/stdc++.h>
using namespace std;
int main() {
  string people[100][2];
  int priority[100];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> people[i][0] >> people[i][1];
  }
  for (int i = 0; i < n; i++) {
    if (people[i][1] == "captain") {
      priority[i] = 4;
    }
    if (people[i][1] == "woman") {
      priority[i] = 2;
    }
    if (people[i][1] == "child") {
      priority[i] = 2;
    }
    if (people[i][1] == "rat") {
      priority[i] = 1;
    }
    if (people[i][1] == "man") {
      priority[i] = 3;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - 1 - i; j++) {
      if (priority[j] > priority[j + 1]) {
        swap(people[j][0], people[j + 1][0]);
        swap(people[j][1], people[j + 1][1]);
        swap(priority[j], priority[j + 1]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << people[i][0] << endl;
  }
}
