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