#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  n++;
  string* arr = new string[n];
  string temparr;
  for (int i = 0; i < n; i++) {
    getline(cin, temparr);
    arr[i] = temparr;
  }
  string x;
  bool woman;
  int* p = new int[n];
  int* q = new int[n];
  for (int i = 1; i < n; i++) {
    x = arr[i];
    q[i] = (i + 1);
    for (int j = 1; j < x.size(); j++) {
      if (x[j] == ' ' && x[j + 1] == 'r') {
        p[i] = 1;
        break;
      }
      if (x[j] == ' ' &&
          ((x[j + 1] == 'w') || ((x[j + 1] == 'c') && (x[j + 2] == 'h')))) {
        p[i] = 2;
        break;
      } else if (x[j] == ' ' && x[j + 1] == 'm') {
        p[i] = 3;
        break;
      } else if (x[j] == ' ' && x[j + 1] == 'c' && x[j + 2] == 'a') {
        p[i] = 4;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < n; j++) {
      if (p[i] < p[j]) {
        string temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        int temp2 = p[i];
        p[i] = p[j];
        p[j] = temp2;
        int temp3 = q[i];
        q[i] = q[j];
        q[j] = temp3;
      } else if (p[i] == p[j]) {
        if (q[i] < q[j]) {
          string tempn = arr[i];
          arr[i] = arr[j];
          arr[j] = tempn;
          int temp2n = p[i];
          p[i] = p[j];
          p[j] = temp2n;
          int temp3n = q[i];
          q[i] = q[j];
          q[j] = temp3n;
        }
      }
    }
  }
  for (int i = 1; i < n; i++) {
    string y = arr[i];
    int c = 8;
    for (int j = 1; j < y.size(); j++) {
      if (y[j] == ' ') {
        c = j;
        break;
      }
    }
    for (int count = 0; count < c; count++) {
      cout << y[count];
    }
    cout << endl;
  }
  return 0;
}
