#include <bits/stdc++.h>
using namespace std;
bool f(pair<int, string> a, pair<int, string> b) {
  if (a.second == b.second || (a.second == "woman" && b.second == "child") ||
      (b.second == "woman" && a.second == "child"))
    return (a.first < b.first);
  if (a.second == "rat") {
    return true;
  }
  if (b.second == "rat") {
    return false;
  }
  if (a.second == "child" || a.second == "woman") {
    return true;
  }
  if (b.second == "child" || b.second == "woman") {
    return false;
  }
  if (a.second == "man") {
    return true;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, string> > s(n);
  vector<string> t(n);
  for (int i = 0; i < n; i++) {
    s[i].first = i;
    cin >> t[i] >> s[i].second;
  }
  sort(s.begin(), s.end(), f);
  for (int i = 0; i < n; i++) {
    cout << t[s[i].first] << endl;
  }
  return 0;
}
