#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
                                    !is_same<T_container, string>::value,
                                    typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
  os << '{';
  string sep;
  for (const T &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
void dbg_out() { cerr << "\n"; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
bool cmp(const pair<pair<long long, long long>, string> &a,
         const pair<pair<long long, long long>, string> &b) {
  if (a.first.first != b.first.first)
    return a.first.first < b.first.first;
  else
    return a.first.second < b.first.second;
}
void solve() {
  long long n;
  cin >> n;
  vector<pair<pair<long long, long long>, string>> v;
  for (long long i = 0; i < n; i++) {
    string a, b;
    cin >> a >> b;
    if (b == "rat")
      v.push_back({{0, i}, a});
    else if (b == "woman" || b == "child")
      v.push_back({{1, i}, a});
    else if (b == "man")
      v.push_back({{2, i}, a});
    else
      v.push_back({{3, i}, a});
  };
  sort(v.begin(), v.end(), cmp);
  ;
  for (auto c : v) cout << c.second << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}
