#include <bits/stdc++.h>
using namespace std;
int main() {
  string name[200], stat[200];
  long n, i;
  cin >> n;
  for (i = 0; i < n; ++i) cin >> name[i] >> stat[i];
  for (i = 0; i < n; ++i)
    if (stat[i].compare("rat") == 0) cout << name[i] << endl;
  for (i = 0; i < n; ++i)
    if (stat[i].compare("woman") == 0 || stat[i].compare("child") == 0)
      cout << name[i] << endl;
  for (i = 0; i < n; ++i)
    if (stat[i].compare("man") == 0) cout << name[i] << endl;
  for (i = 0; i < n; ++i)
    if (stat[i].compare("captain") == 0) cout << name[i] << endl;
  return 0;
}
