#include <bits/stdc++.h>
using namespace std;
bool valley(int p[], int k) {
  if (p[k] < p[k - 1] && p[k] < p[k + 1]) return true;
  return false;
}
bool peak(int p[], int k) {
  if (p[k] > p[k - 1] && p[k] > p[k + 1]) return true;
  return false;
}
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int prime(int b) {
  for (int i = 2; i < b; i++) {
    if (b % i == 0) return 0;
  }
  return 1;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<string> cap, wom, rat, man;
  for (int i = 0; i < n; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    if (s2 == "captain")
      cap.push_back(s1);
    else if (s2 == "woman" || s2 == "child")
      wom.push_back(s1);
    else if (s2 == "rat")
      rat.push_back(s1);
    else
      man.push_back(s1);
  }
  for (int i = 0; i < rat.size(); i++) cout << rat[i] << "\n";
  for (int i = 0; i < wom.size(); i++) cout << wom[i] << "\n";
  for (int i = 0; i < man.size(); i++) cout << man[i] << "\n";
  for (int i = 0; i < cap.size(); i++) cout << cap[i] << "\n";
}
