#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using namespace std;
string a[100], b[100];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    if (b[i] == "rat") {
      cout << a[i] << endl;
      b[i] = "";
    }
  }
  for (int i = 0; i < n; i++) {
    if (b[i] == "child" || b[i] == "woman") {
      cout << a[i] << endl;
      b[i] = "";
    }
  }
  for (int i = 0; i < n; i++) {
    if (b[i] == "man") {
      cout << a[i] << endl;
      b[i] = "";
    }
  }
  for (int i = 0; i < n; i++) {
    if (b[i] == "captain") {
      cout << a[i] << endl;
      break;
    }
  }
  return 0;
}
