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