#include <bits/stdc++.h>
int main() {
  int n;
  char s[101][101], sr[101][101];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%s%s", s[i], sr[i]);
  for (int i = 0; i < n; i++) {
    if (sr[i][0] == 'r') printf("%s\n", s[i]);
  }
  for (int i = 0; i < n; i++) {
    if (sr[i][0] == 'w' || sr[i][0] == 'c' && sr[i][1] == 'h')
      printf("%s\n", s[i]);
  }
  for (int i = 0; i < n; i++) {
    if (sr[i][0] == 'm') printf("%s\n", s[i]);
  }
  for (int i = 0; i < n; i++) {
    if (sr[i][0] == 'c' && sr[i][1] == 'a') printf("%s\n", s[i]);
  }
  return 0;
}
