#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long N = 1e5 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n;
  cin >> n;
  vector<pair<string, string>> v;
  for (int i = 0; i < n; i += 1) {
    string s1, s2;
    cin >> s1 >> s2;
    v.push_back(make_pair(s1, s2));
  }
  for (auto i : v)
    if (i.second == "rat") cout << i.first << '\n';
  for (auto i : v)
    if (i.second == "woman" || i.second == "child") cout << i.first << '\n';
  for (auto i : v)
    if (i.second == "man") cout << i.first << '\n';
  for (auto i : v)
    if (i.second == "captain") cout << i.first << '\n';
  return 0;
}
