#include <bits/stdc++.h>
using namespace std;
vector<pair<string, string> > arr;
bool prior(string a, string b) {
  if (a == "captain")
    return false;
  else if (b == "rat" && a != "rat")
    return false;
  else if (a == "man" && (b == "woman" || b == "child"))
    return false;
  else
    return true;
}
void swapit(int i, int j) {
  pair<string, string> temp;
  temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}
int main(int argc, char** argv) {
  int n;
  cin >> n;
  int i;
  string name, type;
  for (int i = 0; i < n; i++) {
    cin >> name >> type;
    arr.push_back(make_pair(name, type));
  }
  while (true) {
    i = 0;
    bool in = false;
    while (i < n - 1) {
      if (!prior(arr[i].second, arr[i + 1].second)) {
        swapit(i, i + 1);
        in = true;
      }
      i++;
    }
    if (!in) break;
  }
  for (int i = 0; i < n; i++) {
    cout << arr[i].first << endl;
  }
  return 0;
}
