#include <bits/stdc++.h>
using namespace std;
vector<int> pri;
void sieve() {
  bool f[1000007];
  f[0] = f[1] = 1;
  for (int i = 2; i < 1000007; ++i) {
    if (f[i]) continue;
    pri.push_back(i);
    for (int j = i * i; i <= 10000 && j < 1000007; j += i) f[j] = 1;
  }
}
bool prime(int x) {
  if (x == 2) return true;
  if (x < 2 || x % 2 == 0) return false;
  for (int i = 3; i * i <= x; i += 2)
    if (x % i == 0) return false;
  return true;
}
vector<int> factor;
void GetFactor(int n) {
  for (int i = 2; i <= n / i; ++i) {
    while (n % i == 0) {
      factor.push_back(i);
      n /= i;
    }
  }
  if (n != 1) factor.push_back(n);
}
long long MaxSubarray(long long arr[], int n) {
  long long best = -1e18, sum = 0;
  for (int i = 0; i < n; ++i) {
    sum = max(arr[i], arr[i] + sum);
    best = max(best, sum);
  }
  return best;
}
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
long long fast_power(long long b, long long p) {
  long long res = 1;
  while (p) {
    if (p % 2) res = (res * b) % 1000000007;
    b = (b * b) % 1000000007;
    p /= 2;
  }
  return res;
}
bool isvalid(int r, int c, int R, int C) {
  return (r >= 0 && r < R && c >= 0 && c < C);
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
  string name, kind;
  deque<string> Women_Child, men, rats;
  string captin;
  int n;
  cin >> n;
  while (n--) {
    cin >> name >> kind;
    if (kind == "captain")
      captin = name;
    else if (kind == "man")
      men.push_back(name);
    else if (kind == "woman" || kind == "child")
      Women_Child.push_back(name);
    else
      rats.push_back(name);
  }
  for (int i = 0; i < rats.size(); ++i) cout << rats[i] << endl;
  for (int i = 0; i < Women_Child.size(); ++i) cout << Women_Child[i] << endl;
  for (int i = 0; i < men.size(); ++i) cout << men[i] << endl;
  cout << captin;
  return 0;
}
