#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<string> men;
  vector<string> rats;
  vector<string> women;
  string captain;
  int mrat;
  string x, y;
  cin >> mrat;
  for (int i = 0; i < mrat; i++) {
    cin >> x >> y;
    if (y == "captain") {
      captain = x;
    } else if (y == "woman" || y == "child") {
      women.push_back(x);
    } else if (y == "man") {
      men.push_back(x);
    } else if (y == "rat") {
      rats.push_back(x);
    }
  }
  int sm = men.size(), sw = women.size(), sr = rats.size();
  for (int i = 0; i < sr; i++) {
    cout << rats[i] << endl;
  }
  for (int i = 0; i < sw; i++) {
    cout << women[i] << endl;
  }
  for (int i = 0; i < sm; i++) {
    cout << men[i] << endl;
  }
  cout << captain;
  return 0;
}
