#include <bits/stdc++.h>
using namespace std;
bool light[123456];
vector<int> v[123456];
bool visited[123456];
int connected[123456];
int main() {
  int n;
  cin >> n;
  string s, t;
  vector<string> rat, man, woman, child, capt;
  for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
       i += 1 - 2 * ((0) > (n))) {
    cin >> s >> t;
    if (t == "rat")
      rat.push_back(s);
    else if (t == "captain")
      capt.push_back(s);
    else if (t == "woman" || t == "child")
      woman.push_back(s);
    else if (t == "man")
      man.push_back(s);
  }
  for (__typeof(rat.size()) i = (0) - ((0) > (rat.size()));
       i != (rat.size()) - ((0) > (rat.size()));
       i += 1 - 2 * ((0) > (rat.size())))
    cout << rat[i] << endl;
  for (__typeof(woman.size()) i = (0) - ((0) > (woman.size()));
       i != (woman.size()) - ((0) > (woman.size()));
       i += 1 - 2 * ((0) > (woman.size())))
    cout << woman[i] << endl;
  for (__typeof(man.size()) i = (0) - ((0) > (man.size()));
       i != (man.size()) - ((0) > (man.size()));
       i += 1 - 2 * ((0) > (man.size())))
    cout << man[i] << endl;
  cout << capt[0] << endl;
}
