#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  queue<string> rat, woman, man, captin;
  string a, b;
  while (n--) {
    cin >> a >> b;
    if (b == "woman" || b == "child")
      woman.push(a);
    else if (b == "rat")
      rat.push(a);
    else if (b == "man")
      man.push(a);
    else
      captin.push(a);
  }
  while (rat.size()) {
    cout << rat.front() << endl;
    rat.pop();
  }
  while (woman.size()) {
    cout << woman.front() << endl;
    woman.pop();
  }
  while (man.size()) {
    cout << man.front() << endl;
    man.pop();
  }
  while (captin.size()) {
    cout << captin.front() << endl;
    captin.pop();
  }
}
