#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;
const int MAXL = 2000;
char rats[MAXN][MAXL];
char women[MAXN][MAXL];
char man[MAXN][MAXL];
char cap[MAXN][MAXL];
int compPos(char *dst) {
  if (!strcmp("captain", dst)) {
    return 3;
  }
  if (!strcmp("man", dst)) {
    return 2;
  }
  if (!strcmp("woman", dst)) {
    return 1;
  }
  if (!strcmp("child", dst)) {
    return 1;
  }
  if (!strcmp("rat", dst)) {
    return 0;
  }
}
int main() {
  int n;
  char name[MAXL];
  char pos[MAXL];
  while (scanf("%d", &n) != EOF) {
    int iWomen = 0, iRats = 0, iMan = 0, iCap = 0;
    memset(name, 0, sizeof(name));
    memset(pos, 0, sizeof(pos));
    memset(rats, 0, sizeof(rats));
    memset(women, 0, sizeof(women));
    memset(man, 0, sizeof(man));
    memset(cap, 0, sizeof(cap));
    for (int i = 0; i < n; i++) {
      scanf("%s %s", name, pos);
      switch (compPos(pos)) {
        case 0:
          strcpy(rats[iRats++], name);
          break;
        case 1:
          strcpy(women[iWomen++], name);
          break;
        case 2:
          strcpy(man[iMan++], name);
          break;
        case 3:
          strcpy(cap[iCap++], name);
          break;
      }
    }
    for (int i = 0; i < iRats; i++) {
      printf("%s\n", rats[i]);
    }
    for (int i = 0; i < iWomen; i++) {
      printf("%s\n", women[i]);
    }
    for (int i = 0; i < iMan; i++) {
      printf("%s\n", man[i]);
    }
    for (int i = 0; i < iCap; i++) {
      printf("%s\n", cap[i]);
    }
  }
  return 0;
}
