#include <bits/stdc++.h>
using namespace std;
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  fast();
  map<int, pair<string, string> > mp;
  int n;
  cin >> n;
  string a, s;
  for (int i = 0; i < n; i++) {
    cin >> a >> s;
    mp[i].first = a;
    mp[i].second = s;
  }
  for (int i = 0; i < n - 1; i++) {
    if (mp[i].second == "captain") {
      for (int j = i; j < n - 1; j++) swap(mp[j], mp[j + 1]);
    }
  }
  for (int i = 0; i < n; i++) {
    if (mp[i].second == "rat") {
      for (int j = i; j > 0; j--)
        if (mp[j - 1].second != "rat") swap(mp[j], mp[j - 1]);
    }
  }
  for (int i = 0; i < n; i++) {
    if (mp[i].second == "woman" || mp[i].second == "child") {
      for (int j = i; j > 0; j--)
        if (mp[j - 1].second != "rat" && mp[j - 1].second != "woman" &&
            mp[j - 1].second != "child")
          swap(mp[j], mp[j - 1]);
    }
  }
  for (auto it = mp.begin(); it != mp.end(); it++) {
    cout << it->second.first << endl;
  }
  return 0;
}
