#include <bits/stdc++.h>
using namespace std;
pair<string, string> crew[100];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> crew[i].first >> crew[i].second;
  for (int i = 0; i < n; i++)
    if (crew[i].second == "rat") cout << crew[i].first << endl;
  for (int i = 0; i < n; i++)
    if (crew[i].second == "woman" || crew[i].second == "child")
      cout << crew[i].first << endl;
  for (int i = 0; i < n; i++)
    if (crew[i].second == "man") cout << crew[i].first << endl;
  for (int i = 0; i < n; i++)
    if (crew[i].second == "captain") cout << crew[i].first << endl;
  return 0;
}
