#include <bits/stdc++.h>
using namespace std;
int n, c = 0;
string a[1001], second[1001], t[1001];
int main() {
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    cin >> second[i] >> t[i];
    if (t[i] == "rat") a[++c] = second[i];
    if (t[i] == "captain") a[n] = second[i];
  }
  for (int i = 1; i <= n; i++) {
    if (t[i] == "woman" || t[i] == "child") a[++c] = second[i];
  }
  for (int i = 1; i <= n; i++) {
    if (t[i] == "man") a[++c] = second[i];
  }
  for (int i = 1; i <= n; i++) {
    cout << a[i] << endl;
  }
  fclose(stdin);
  fclose(stdout);
  return 0;
}
