#include <bits/stdc++.h>
using namespace std;
string A[5] = {"rat", "woman", "child", "man", "captain"};
int val[] = {0, 1, 1, 2, 3};
string s[100000];
string t[100000];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> s[i] >> t[i];
  vector<pair<int, int> > v;
  for (int i = 0; i < n; i++) {
    int type;
    for (int j = 0; j < 5; ++j)
      if (t[i] == A[j]) type = val[j];
    v.push_back(make_pair(type, i));
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) cout << s[v[i].second] << endl;
}
