#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long INFL = LLONG_MAX;
const long double pi = acos(-1);
vector<string> captain;
vector<string> man;
vector<string> rat;
vector<string> woman;
int main() {
  ios_base::sync_with_stdio(0);
  cout.precision(15);
  cout << fixed;
  cout.tie(0);
  int N;
  cin >> N;
  for (int(i) = 1, j123 = N; (i) <= j123; (i)++) {
    string s, t;
    cin >> s >> t;
    if (t == "captain") captain.push_back(s);
    if (t == "man") man.push_back(s);
    if (t == "rat") rat.push_back(s);
    if (t == "child" || t == "woman") woman.push_back(s);
  }
  for (int(i) = 0, j123 = int(rat.size()); (i) < j123; (i)++)
    cout << rat[i] << '\n';
  for (int(i) = 0, j123 = int(woman.size()); (i) < j123; (i)++)
    cout << woman[i] << '\n';
  for (int(i) = 0, j123 = int(man.size()); (i) < j123; (i)++)
    cout << man[i] << '\n';
  for (int(i) = 0, j123 = int(captain.size()); (i) < j123; (i)++)
    cout << captain[i] << '\n';
}
