#include <bits/stdc++.h>
using namespace std;
vector<pair<int, string> > crew;
void print(const int x) {
  for (const auto &it : crew) {
    if (it.first == x) cout << it.second << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  crew.resize(n);
  for (int i = 0; i < n; ++i) {
    string name, role;
    cin >> name >> role;
    if (role == "rat")
      crew[i] = {0, name};
    else if (role == "woman" || role == "child")
      crew[i] = {1, name};
    else if (role == "man")
      crew[i] = {2, name};
    else
      crew[i] = {3, name};
  }
  for (int i = 0; i < 4; ++i) {
    print(i);
  }
  return 0;
}
