#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(NULL),
      std::cout.tie(NULL);
  map<string, long long> pr;
  pr["rat"] = 0;
  pr["woman"] = pr["child"] = 1;
  pr["man"] = 2;
  pr["captain"] = 3;
  vector<pair<long long, string> > vec[4];
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    string name, type;
    cin >> name >> type;
    vec[pr[type]].push_back(std::make_pair(i, name));
  }
  for (long long i = 0; i < 4; i++) sort(vec[i].begin(), vec[i].end());
  for (long long i = 0; i < 4; i++)
    for (long long j = 0; j < vec[i].size(); j++)
      cout << vec[i][j].second << endl;
  return 0;
}
