#include <bits/stdc++.h>
char pp[100];
char tt[100];
char woman[100][100];
char man[100][100];
char captain[100][100];
char rabbit[100][100];
int main() {
  int n, len;
  while (scanf("%d", &n) != EOF) {
    getchar();
    int i, j, cntr = 0, cntw = 0, cntm = 0, cntc = 0;
    while (n--) {
      scanf("%s%s", pp, tt);
      if (strcmp(tt, "rat") == 0) {
        len = strlen(pp);
        for (j = 0; j < len; j++) rabbit[cntr][j] = pp[j];
        rabbit[cntr][len] = '\0';
        cntr++;
      } else if (strcmp(tt, "woman") == 0) {
        len = strlen(pp);
        for (j = 0; j < len; j++) woman[cntw][j] = pp[j];
        woman[cntw][len] = '\0';
        cntw++;
      } else if (strcmp(tt, "child") == 0) {
        len = strlen(pp);
        for (j = 0; j < len; j++) woman[cntw][j] = pp[j];
        woman[cntw][len] = '\0';
        cntw++;
      } else if (strcmp(tt, "man") == 0) {
        len = strlen(pp);
        for (j = 0; j < len; j++) man[cntm][j] = pp[j];
        man[cntm][len] = '\0';
        cntm++;
      } else {
        len = strlen(pp);
        for (j = 0; j < len; j++) captain[cntc][j] = pp[j];
        captain[cntc][len] = '\0';
        cntc++;
      }
    }
    for (i = 0; i < cntr; i++) printf("%s\n", rabbit[i]);
    for (i = 0; i < cntw; i++) printf("%s\n", woman[i]);
    for (i = 0; i < cntm; i++) printf("%s\n", man[i]);
    for (i = 0; i < cntc; i++) printf("%s\n", captain[i]);
  }
  return 0;
}
