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