#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string first, secound;
  string arr1[100], arr2[100], arr3[100], arr4[100];
  int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0;
  for (int i = 0; i < n; i++) {
    cin >> first >> secound;
    if (secound == "rat")
      arr1[counter1++] = first;
    else if ((secound == "woman") || (secound == "child")) {
      arr2[counter2++] = first;
    } else if (secound == "man") {
      arr3[counter3++] = first;
    } else {
      arr4[counter4++] = first;
    }
  }
  for (int k = 0; k < counter1; k++) cout << arr1[k] << endl;
  for (int i = 0; i < counter2; i++) {
    cout << arr2[i] << endl;
  }
  for (int j = 0; j < counter3; j++) {
    cout << arr3[j] << endl;
  }
  for (int m = 0; m < counter4; m++) cout << arr4[m] << endl;
  return 0;
}
