#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  char a[100][20], b[100][20];
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%s%s", a[i], b[i]);
  for (i = 0; i < n; i++)
    if (!strcmp(b[i], "rat")) puts(a[i]);
  for (i = 0; i < n; i++)
    if (!strcmp(b[i], "woman") || !strcmp(b[i], "child")) puts(a[i]);
  for (i = 0; i < n; i++)
    if (!strcmp(b[i], "man")) puts(a[i]);
  for (i = 0; i < n; i++)
    if (!strcmp(b[i], "captain")) puts(a[i]);
}
