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