#include <bits/stdc++.h>
using namespace std;
template <class T>
inline vector<pair<T, int> > FACTORISE(T n) {
  vector<pair<T, int> > R;
  for (T i = 2; n > 1;) {
    if (n % i == 0) {
      int C = 0;
      for (; n % i == 0; C++, n /= i)
        ;
      R.push_back(make_pair(i, C));
    }
    i++;
    if (i > n / i) i = n;
  }
  if (n > 1) R.push_back(make_pair(n, 1));
  return R;
}
template <class T>
inline T TOTIENT(T n) {
  vector<pair<T, int> > R = FACTORISE(n);
  T r = n;
  for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1);
  return r;
}
struct state {
  int row, col, val;
  state(int _r, int _c, int _v) : row(_r), col(_c), val(_v) {}
  bool operator<(const state &s) const { return val > s.val; }
};
int main() {
  int a[105];
  int n;
  scanf("%d", &n);
  vector<string> v;
  string ss;
  string ss2;
  for (int i = 0; i < n; i++) {
    cin >> ss >> ss2;
    if (ss2 == "rat")
      a[i] = 0;
    else if (ss2 == "child")
      a[i] = 1;
    else if (ss2 == "woman")
      a[i] = 1;
    else if (ss2 == "man")
      a[i] = 2;
    else
      a[i] = 3;
    v.push_back(ss);
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 0) {
      cout << v[i] << endl;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      cout << v[i] << endl;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 2) {
      cout << v[i] << endl;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 3) {
      cout << v[i] << endl;
    }
  }
  return 0;
}
