#include <bits/stdc++.h>
using namespace std;
int main() {
  string p;
  int i, n;
  cin >> n;
  pair<int, int> ans[n];
  string s[n];
  for (i = 0; i < n; i++) {
    cin >> s[i] >> p;
    ans[i].second = i;
    if (p == "rat")
      ans[i].first = 0;
    else if (p == "man")
      ans[i].first = 2;
    else if (p == "captain")
      ans[i].first = 3;
    else
      ans[i].first = 1;
  }
  sort(ans, ans + n);
  for (i = 0; i < n; i++) cout << s[ans[i].second] << endl;
}
