#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<string> a[5];
  while (n--) {
    string x, y;
    cin >> x >> y;
    if (y == "rat")
      a[0].push_back(x);
    else if (y == "woman" || y == "child")
      a[1].push_back(x);
    else if (y == "man")
      a[2].push_back(x);
    else if (y == "captain")
      a[3].push_back(x);
  }
  for (long long i = 0; i < 4; i++) {
    for (long long j = 0; j < a[i].size(); j++) cout << a[i][j] << endl;
  }
}
