#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  char name[100][11], dol[100][11];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> name[i] >> dol[i];
  for (int i = 0; i < n; i++) {
    if (dol[i][0] == 'r') cout << name[i] << endl;
  };
  for (int i = 0; i < n; i++) {
    if ((dol[i][0] == 'w') || ((dol[i][0] == 'c') && (dol[i][1] == 'h')))
      cout << name[i] << endl;
  };
  for (int i = 0; i < n; i++) {
    if (dol[i][0] == 'm') cout << name[i] << endl;
  };
  for (int i = 0; i < n; i++) {
    if ((dol[i][0] == 'c') && (dol[i][1] == 'a')) cout << name[i] << endl;
  };
  return 0;
}
