Input Format
The first line contains the integer , the number of people.
lines follow each containing the space separated values of the first name, last name, age and sex, respectively.
Constraints
Output Format
Output  names on separate lines in the format described above in ascending order of age.

### Problem solution in Python 3 programming language.

def formal(ns):
first,last,age,sex = ns
return ('Mr. ' if sex == 'M' else 'Ms. ') + first + ' ' + last

def std(f):
def inner(ns):
return map(formal, f(ns))
return inner

@std
def nsort(ns):
return [x[1] for x in sorted(enumerate(ns), key=lambda x: (x[1][2],x[0]))]

n = int(input())
for x in nsort([input().split() for x in range(n)]):
print(x)

### Second solution

import operator
def person_lister(f):
def inner(people):
return map(f, sorted(people, key=lambda x: int(x[2])))
return inner
@person_lister
def name_format(person):
return ("Mr. " if person[3] == "M" else "Ms. ") + person[0] + " " + person[1]
if __name__ == '__main__':
people = [input().split() for i in range(int(input()))]
print(*name_format(people), sep='\n')

### Problem solution in Python 2 programming language.

def formal(ns):
first,last,age,sex = ns
return ('Mr. ' if sex == 'M' else 'Ms. ') + first + ' ' + last

def std(f):
def inner(ns):
return map(formal, f(ns))
return inner

@std
def nsort(ns):
return [x[1] for x in sorted(enumerate(ns), key=lambda x: (x[1][2],x[0]))]

n = int(raw_input())
for x in nsort([raw_input().split() for x in range(n)]):
print x