#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  int c[n], d[n], e[n], f, temp = 0, temp1 = 0, temp2 = 0;
  for (int i = 0; i < n; i++) {
    if (b[i] == "rat") {
      c[temp] = i;
      temp++;
    } else if (b[i] == "woman" || b[i] == "child") {
      d[temp1] = i;
      temp1++;
    } else if (b[i] == "man") {
      e[temp2] = i;
      temp2++;
    } else {
      f = i;
    }
  }
  for (int i = 0; i < temp; i++) {
    cout << a[c[i]] << endl;
  }
  for (int i = 0; i < temp1; i++) {
    cout << a[d[i]] << endl;
  }
  for (int i = 0; i < temp2; i++) {
    cout << a[e[i]] << endl;
  }
  cout << a[f] << endl;
  return 0;
}
