#include <bits/stdc++.h>
using namespace std;
pair<string, string> arr[100];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    char name[11], status[9];
    scanf("%s%s", &name, &status);
    arr[i].first = string(name);
    arr[i].second = string(status);
  }
  for (int i = 0; i < n; ++i) {
    if (arr[i].second == "rat") {
      printf("%s\n", arr[i].first.c_str());
    }
  }
  for (int i = 0; i < n; ++i) {
    if (arr[i].second == "woman" || arr[i].second == "child") {
      printf("%s\n", arr[i].first.c_str());
    }
  }
  for (int i = 0; i < n; ++i) {
    if (arr[i].second == "man") {
      printf("%s\n", arr[i].first.c_str());
    }
  }
  for (int i = 0; i < n; ++i) {
    if (arr[i].second == "captain") {
      printf("%s\n", arr[i].first.c_str());
    }
  }
  return 0;
}
