n=int(input())
l=[]
n1=0n2=0n3=0for i in range(n):
b=[a for a in input().split()]
if b[1]=='rat':
l.insert(n1,b[0])
n1+=1
n2+=1
n3+=1
elif b[1]=='woman' or b[1]=='child':
l.insert(n2,b[0])
n2+=1
n3+=1
elif b[1]=='captain':
l.append(b[0])
else:
l.insert(n3,b[0])
n3+=1
print('\n'.join(l))
/*
author : Antar chandra Nath
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long int lli;
#define fast ios::sync_with_stdio(0);cin.tie(0);cin.tie(0);
#define endl "\n"
#define pb push_back
#define po pop_back
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define cn cin >>
#define co cout <<
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define loop(i,j,n) for(int i=j;i<=n;i++)
#define vi vector<int>
#define vvi vector<vector<int>>
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define mci map<char, int>
#define mii map<int, int>
#define pi pair<int, int>
#define pll pair<ll, ll>
int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
const int maxn = 1e5 + 1;
const int mm = 1e9 + 9;
const ll mmm = 1e18 + 18;
ll gcd(ll a, ll b) {return __gcd(a, b);}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b);}
void solve(){
int n;
cin >> n;
vector<string> a(n), b(n);
vector<string> v = {"rat", "woman", "man", "captain"};
map<string, vector<string> > mp;
rep(i, n){
cin >> a[i] >> b[i];
if(b[i] == "woman" or b[i] == "child")
mp["woman"].pb(a[i]);
else
mp[b[i]].pb(a[i]);
}
rep(i, 4){
rep(j, mp[v[i]].size()){
cout << mp[v[i]][j] << endl;
}
}
return;
}
int main(){
fast;
int t(1);
// cn t;
while(t--){
solve();
}
return 0;
}
129. Sum Root to Leaf Numbers | 120. Triangle |
102. Binary Tree Level Order Traversal | 96. Unique Binary Search Trees |
75. Sort Colors | 74. Search a 2D Matrix |
71. Simplify Path | 62. Unique Paths |
50. Pow(x, n) | 43. Multiply Strings |
34. Find First and Last Position of Element in Sorted Array | 33. Search in Rotated Sorted Array |
17. Letter Combinations of a Phone Number | 5. Longest Palindromic Substring |
3. Longest Substring Without Repeating Characters | 1312. Minimum Insertion Steps to Make a String Palindrome |
1092. Shortest Common Supersequence | 1044. Longest Duplicate Substring |
1032. Stream of Characters | 987. Vertical Order Traversal of a Binary Tree |
952. Largest Component Size by Common Factor | 212. Word Search II |
174. Dungeon Game | 127. Word Ladder |
123. Best Time to Buy and Sell Stock III | 85. Maximal Rectangle |
84. Largest Rectangle in Histogram | 60. Permutation Sequence |
42. Trapping Rain Water | 32. Longest Valid Parentheses |