#include <bits/stdc++.h>
using namespace std;
int prior(string a) {
  if (a == "rat") {
    return 0;
  } else if (a == "woman" || a == "child") {
    return 1;
  } else if (a == "man") {
    return 2;
  } else
    return 3;
}
int main() {
  int n, i, j;
  int k[4] = {0};
  string b[4][100];
  cin >> n;
  string a[100][2];
  for (i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  for (i = 0; i < n; i++) {
    b[prior(a[i][1])][k[prior(a[i][1])]] = a[i][0];
    k[prior(a[i][1])]++;
  }
  for (i = 0; i < 4; i++) {
    for (j = 0; j < k[i]; j++) {
      cout << b[i][j] << endl;
    }
  }
  return 0;
}
