#include <bits/stdc++.h>
using namespace std;
struct Being {
  string name;
  long long p, i;
};
Being A[105];
bool comp(Being a, Being b) {
  if (a.p == b.p) {
    return a.i < b.i;
  } else {
    return a.p < b.p;
  }
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> A[i].name;
    string s;
    cin >> s;
    if (s == "rat") {
      A[i].p = 1;
    } else if (s == "woman") {
      A[i].p = 2;
    } else if (s == "child") {
      A[i].p = 2;
    } else if (s == "man") {
      A[i].p = 3;
    } else if (s == "captain") {
      A[i].p = 4;
    }
    A[i].i = i;
  }
  sort(A, A + n, comp);
  for (int i = 0; i < n; i++) {
    cout << A[i].name << endl;
  }
}
