#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string estado, nombre;
  cin >> n;
  vector<pair<string, string> > lista;
  vector<string> ratas;
  vector<string> mujer;
  vector<string> hombre;
  vector<string> capitan;
  for (int i = 0; i < n; i++) {
    cin >> nombre;
    cin >> estado;
    lista.push_back(pair<string, string>(nombre, estado));
  }
  for (int i = 0; i < n; i++) {
    if (lista[i].second == "rat")
      ratas.push_back(lista[i].first);
    else if (lista[i].second == "child" || lista[i].second == "woman")
      mujer.push_back(lista[i].first);
    else if (lista[i].second == "captain")
      capitan.push_back(lista[i].first);
    else if (lista[i].second == "man")
      hombre.push_back(lista[i].first);
  }
  for (int i = 0; i < int(ratas.size()); i++) {
    cout << ratas[i] << endl;
  }
  for (int i = 0; i < int(mujer.size()); i++) {
    cout << mujer[i] << endl;
  }
  for (int i = 0; i < int(hombre.size()); i++) {
    cout << hombre[i] << endl;
  }
  for (int i = 0; i < int(capitan.size()); i++) {
    cout << capitan[i] << endl;
  }
  return 0;
}
