#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<string> vM;
  vector<string> vCA;
  vector<string> vWCH;
  vector<string> vR;
  for (int i = 0; i < n; i++) {
    string a, b;
    cin >> a >> b;
    if (b[0] == 'r') {
      vR.push_back(a);
    } else if (b[0] == 'm') {
      vM.push_back(a);
    } else if ((b[0] == 'c' && b[1] == 'h') || b[0] == 'w') {
      vWCH.push_back(a);
    } else if (b[0] == 'c' && b[1] == 'a') {
      vCA.push_back(a);
    }
  }
  n = vR.size();
  for (int i = 0; i < n; i++) {
    cout << vR[i] << endl;
  }
  n = vWCH.size();
  for (int i = 0; i < n; i++) {
    cout << vWCH[i] << endl;
  }
  n = vM.size();
  for (int i = 0; i < n; i++) {
    cout << vM[i] << endl;
  }
  n = vCA.size();
  for (int i = 0; i < n; i++) {
    cout << vCA[i] << endl;
  }
  return 0;
}
