#include <bits/stdc++.h>
using namespace std;
struct st1 {
  char s1[15];
  char s2[10];
  int ut, id;
};
st1 a[110];
int n;
bool dk(st1 t1, st1 t2) {
  if (t1.ut < t2.ut) return t1.ut < t2.ut;
  if (t1.ut == t2.ut && t1.id < t2.id) return t1.id < t2.id;
  return 0;
}
void solve() {
  int i;
  for (i = 0; i < n; i++) {
    scanf("%s %s", &a[i].s1, &a[i].s2);
    a[i].id = i;
    if (strcmp(a[i].s2, "rat") == 0) a[i].ut = 1;
    if (strcmp(a[i].s2, "child") == 0 || strcmp(a[i].s2, "woman") == 0)
      a[i].ut = 2;
    if (strcmp(a[i].s2, "man") == 0) a[i].ut = 3;
    if (strcmp(a[i].s2, "captain") == 0) a[i].ut = 4;
  }
  sort(a, a + n, dk);
  for (i = 0; i < n; i++) printf("%s\n", a[i].s1);
}
int main() {
  while (scanf("%d", &n) > 0) {
    solve();
  }
  return 0;
}
