#include <bits/stdc++.h>
using namespace std;
void speed() {
  ios::sync_with_stdio(NULL);
  cin.tie(0);
  cout.tie(0);
}
long long n, m, k, cnt, ans, sum, x, y, mx, mn = 1e9;
string s, s1, s2, a, b, c;
vector<int> v;
set<char> st;
multimap<string, string> mp;
int main() {
  speed();
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    if (s2 == "rat") a += s1 + '\n';
    if (s2 == "woman" or s2 == "child") b += s1 + '\n';
    if (s2 == "man") c += s1 + '\n';
    if (s2 == "captain") s += s1 + '\n';
  }
  cout << a << b << c << s;
}
