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