#include <bits/stdc++.h>
using namespace std;
int main() {
  char crew[100][20], desig[100][8];
  char c0[] = "rat";
  char c1[] = "woman";
  char c2[] = "child";
  char c3[] = "man";
  char c4[] = "captain";
  int n, i, a[100];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> crew[i];
    cin >> desig[i];
  }
  for (i = 0; i < n; i++) {
    if (strcmp(desig[i], c0) == 0) {
      a[i] = 1;
    } else if (strcmp(desig[i], c1) == 0) {
      a[i] = 2;
    } else if (strcmp(desig[i], c2) == 0) {
      a[i] = 2;
    } else if (strcmp(desig[i], c3) == 0) {
      a[i] = 3;
    } else {
      a[i] = 4;
    }
  }
  for (i = 0; i < n; i++) {
    if (a[i] == 1) cout << crew[i] << endl;
  }
  for (i = 0; i < n; i++) {
    if (a[i] == 2) cout << crew[i] << endl;
  }
  for (i = 0; i < n; i++) {
    if (a[i] == 3) cout << crew[i] << endl;
  }
  for (i = 0; i < n; i++) {
    if (a[i] == 4) cout << crew[i] << endl;
  }
  return 0;
}
