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