#include <bits/stdc++.h>
using namespace std;
int i, n;
string b[101], a[101], c[101];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i] >> b[i];
  for (i = 1; i <= n; i++) {
    if (b[i] == "rat") c[1] += a[i], c[1] += "\n";
    if (b[i] == "woman" || b[i] == "child") c[2] += a[i], c[2] += "\n";
    if (b[i] == "man") c[3] += a[i], c[3] += "\n";
    if (b[i] == "captain") c[4] += a[i], c[4] += "\n";
  }
  for (i = 1; i <= 4; i++) cout << c[i];
}
