#include <bits/stdc++.h>
using namespace std;
bool sortin(const pair<long long int, long long int> &e,
            const pair<long long int, long long int> &f) {
  return (e.first < f.first);
}
long long int i, j, k, l, m, n, c, p, q, ts, mn = 10e17;
long long int a[200002], b[200002], x[200002];
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  };
  cin >> n;
  string s, t, u[n], v[n], w[n], y[n];
  for (long long int i = 0; i <= n - 1; i++) {
    cin >> s;
    cin >> t;
    if (t[0] == 'r')
      u[c++] = s;
    else if (t[0] == 'w' || t == "child")
      v[p++] = s;
    else if (t[0] == 'm')
      w[q++] = s;
    else
      y[l++] = s;
  }
  for (long long int i = 0; i <= c - 1; i++) cout << u[i], cout << endl;
  for (long long int i = 0; i <= p - 1; i++) cout << v[i], cout << endl;
  for (long long int i = 0; i <= q - 1; i++) cout << w[i], cout << endl;
  for (long long int i = 0; i <= l - 1; i++) cout << y[i], cout << endl;
}
