Let's use decorators to build a name directory! You are given some information about $N$ people. Each person has a first name, last name, age and sex. Print their names in a specific format sorted by their age in ascending order i.e. the youngest person's name should be printed first. For two people of the same age, print them in the order of their input.

For Henry Davids, the output should be: 

Mr. Henry Davids

For Mary George, the output should be:

Ms. Mary George

Input Format

The first line contains the integer $N$, the number of people.

$N$ lines follow each containing the space separated values of the first name, last name, age and sex, respectively.  

Constraints 

$1\leq N\leq10$

Output Format

Output $N$ names on separate lines in the format described above in ascending order of age. 

Sample Input

3
Mike Thomson 20 M
Robert Bustle 32 M
Andria Bustle 30 F

Sample Output

Mr. Mike Thomson
Ms. Andria Bustle
Mr. Robert Bustle

Concept

For sorting a nested list based on some parameter, you can use the itemgetter library. You can read more about it here.

Source: hackerrank | Difficulty: EASY


Solutions:
from operator import itemgetter
N = int(input())
mylist = []
for _ in range(N):
	inner_list = input().split(' ')
	mylist.append(inner_list)
mylist.sort(key=itemgetter(2))
for i in mylist:
	if i[3] == 'M':
		title = 'Mr. '
	else:
		title = 'Ms. '
	first_name = i[0]
	last_name = i[1]
	print(title + first_name + ' ' + last_name)


import sys
from operator import itemgetter
d = [x.strip().split(' ') for x in sys.stdin]
print(*('{} {} {}'.format('Mr.' if g == 'M' else 'Ms.', f, l) for (f, l, a, g) in sorted(d[1:], key=itemgetter(2))), sep='\n')


def main():
	count = int(input())
	humans = []
	for i in range(count):
		humans.append(str(input()).split(' '))
	humans = sorted(humans, key=lambda person: person[2])
	for human in humans:
		print(('Mr.' if human[3] == 'M' else 'Ms.') + ' ' + human[0] + ' ' + human[1])
main()


from operator import itemgetter
n = int(input())
lis = []
for x in range(n):
	lis.append(input().split())
lis.sort(key=itemgetter(2))
for x in range(len(lis)):
	if lis[x][3] == 'M':
		print('Mr. ' + lis[x][0] + ' ' + lis[x][1])
	else:
		print('Ms. ' + lis[x][0] + ' ' + lis[x][1])


from operator import itemgetter, attrgetter
l = []
n = int(input())
for i in range(0, n):
	s = input()
	if not s:
		break
	l.append(tuple(s.split(' ')))
s1 = sorted(l, key=itemgetter(2))
for i in range(0, len(s1)):
	if s1[i][3] == 'M':
		print('Mr. ' + s1[i][0] + ' ' + s1[i][1])
	else:
		print('Ms. ' + s1[i][0] + ' ' + s1[i][1])


def formatter(f):

	def decorator(rec):
		return f('%s %s %s' % ('Mr.' if rec[3] == 'M' else 'Ms.', rec[0], rec[1]))
	return decorator

@formatter
def printName(rec):
	print(rec)
L = []
for _ in range(0, int(input())):
	L.append(input().split(' '))
L.sort(key=lambda rec: rec[2])
for rec in L:
	printName(rec)


from operator import itemgetter

def parse_person(text):
	(fn, ln, age, sex) = text.split(' ')
	return [fn, ln, int(age), sex]

def print_person(p):
	prefix = ''
	if p[3] == 'M':
		prefix = 'Mr.'
	else:
		prefix = 'Ms.'
	return prefix + ' ' + p[0] + ' ' + p[1]
n = int(input())
people = [parse_person(input()) for p in range(n)]
people.sort(key=itemgetter(2))
output = [print_person(p) for p in people]
print('\n'.join(output))


from operator import itemgetter
N = int(input())
people = []
for i in range(N):
	people.append(input().split())
people.sort(key=itemgetter(2))
for person in people:
	if person[3] == 'M':
		print('Mr. ' + person[0] + ' ' + person[1])
	if person[3] == 'F':
		print('Ms. ' + person[0] + ' ' + person[1])


from operator import itemgetter
n = int(input())
people = []
while n:
	inp = input()
	people.append(inp.split())
	n -= 1

def printer(func):

	def inner(p):
		sp = func(p)
		for i in sp:
			print('Mr.' if i[3] == 'M' else 'Ms.', i[0], i[1])
	return inner

@printer
def printPeople(p):
	p.sort(key=itemgetter(2))
	return p
printPeople(people)


from operator import itemgetter
__author__ = 'mison6'

class Decorator:

	def __init__(self, name_list):
		names = [name.split(' ') for name in name_list]
		names.sort(key=itemgetter(2))
		self._my_names = [' '.join(['Mr.' if name[3] == 'M' else 'Ms.', name[0], name[1]]) for name in names]
		[print(name) for name in self._my_names]
n = int(input())
name_list = [input() for i in range(n)]
Decorator(name_list)


def wrapper(func):

	def formatname(name):
		return [('Mr. ' if j[3] == 'M' else 'Ms. ') + j[0] + ' ' + j[1] for j in [i.split() for i in func(name)]]
	return formatname

@wrapper
def agesort(names):
	return sorted(names, key=lambda x: x.split()[2])
num = int(input())
names = [input() for i in range(0, num)]
for i in agesort(names):
	print(i)


n = int(input())
people = []
for i in range(n):
	(fname, lname, age, sex) = input().split(' ')
	people.append([age, i, fname, lname, sex])
for j in sorted(people):
	if j[4] == 'M':
		print('Mr. ' + j[2] + ' ' + j[3])
	else:
		print('Ms. ' + j[2] + ' ' + j[3])


from operator import itemgetter
n = int(input())
dic = {}
lst = []
for x in range(n):
	temp = input().split()
	lst.append([temp[2], temp[0] + ' ' + temp[1], temp[3]])
lst.sort(key=itemgetter(0))
for x in lst:
	title = 'Mr.'
	if x[2] == 'F':
		title = 'Ms.'
	print(title, x[1])


def normalize(name):
	s = name.split()
	g = 'Mr.'
	if s[3] == 'F':
		g = 'Ms.'
	s[3] = g
	return s

def norm(func):

	def dec(nums):
		normal = [normalize(num) for num in nums]
		normal = func(normal, key=lambda a: a[2])
		return normal
	return dec
c = int(input())
names = []
for i in range(c):
	names.append(input())
sor = norm(sorted)
names = sor(names)
for name in names:
	print(name[3], name[0], name[1])


N = int(input())
l = []
for i in range(N):
	(first, last, age, gender) = input().split()
	l.append([first + ' ' + last, gender, age])
d = {'M': 'Mr.', 'F': 'Ms.'}
l.sort(key=lambda x: x[2])
for i in l:
	print('{} {}'.format(d[i[1]], i[0]))


from operator import itemgetter
people = []
for i in range(int(input())):
	inp = input()
	aux = []
	aux.append(inp.split()[:-2])
	aux.append(inp.split()[-2])
	aux.append(inp.split()[-1])
	people.append(aux)
people.sort(key=itemgetter(1))
for i in people:
	namel = i[0]
	name = namel[0] + ' ' + namel[1]
	if i[2] == 'M':
		print('Mr. ' + name)
	else:
		print('Ms. ' + name)


n = int(input())
a = []
for i in range(n):
	l = input().split(' ')
	a.append(l)
	a.sort(key=lambda x: int(x[2]))
for i in a:
	if i[3] == 'M':
		print('Mr. ' + i[0] + ' ' + i[1])
	else:
		print('Ms. ' + i[0] + ' ' + i[1])


def get_input():
	values = []
	num_lines = int(input())
	for i in range(num_lines):
		values.append(input().split())
	values = sorted(values, key=lambda x: int(x[2]))
	return values

def wrapper(func):

	def create_name_dir():
		values = get_input()
		for i in range(len(values)):
			(f_name, l_name, age, gender) = values[i]
			if str(gender).upper() == 'M':
				values[i] = 'Mr. {} {}'.format(f_name, l_name)
			else:
				values[i] = 'Ms. {} {}'.format(f_name, l_name)
		return values
	return create_name_dir

def main():
	decorated_input = wrapper(get_input)
	values = decorated_input()
	for val in values:
		print(val)
main()


def rename(person):
	if person[3] == 'M':
		person.append('Mr.')
		return person
	else:
		person.append('Ms.')
		return person
N = int(input())
lis = []
for i in range(0, N):
	lis.append(input().split(' '))
lis = list(map(rename, lis))
lis = sorted(lis, key=lambda person: int(person[2]))
for i in range(0, len(lis)):
	print(lis[i][4] + ' ' + lis[i][0] + ' ' + lis[i][1])


from operator import itemgetter
a = int(input())
s = []
for i in range(a):
	s.append(input().split(' '))
s.sort(key=itemgetter(2))
for i in range(len(s)):
	if s[i][3] == 'M':
		print('Mr. {0} {1}'.format(s[i][0], s[i][1]))
	else:
		print('Ms. {0} {1}'.format(s[i][0], s[i][1]))


N = int(input())
res = []
for i in range(N):
	res.append(input().split(' '))
res.sort(key=lambda t: t[2])
for s in res:
	if s[3] == 'M':
		print('Mr. ' + s[0] + ' ' + s[1])
	else:
		print('Ms. ' + s[0] + ' ' + s[1])


import re
sub_input = lambda pattern: lambda func: lambda : func(re.match(pattern, input()))
honor = {'M': 'Mr.', 'F': 'Ms.'}

class User:

	def __init__(self, first, last, age, hon):
		self.first = first
		self.last = last
		self.age = age
		self.hon = hon

	def __str__(self):
		return '{self.hon} {self.first} {self.last}'.format(self=self)
age = lambda user: user.age

@sub_input('(\\w+)\\s+(\\w+)\\s+(\\d+)\\s+(M|F)')
def read_user(match):
	return User(match.group(1), match.group(2), int(match.group(3)), honor[match.group(4)])
for user in sorted((read_user() for i in range(int(input()))), key=age):
	print(user)


N = int(input())
records = []
for i in range(N):
	(firstname, lastname, age, sex) = input().split()
	if sex == 'M':
		name = 'Mr. %s %s' % (firstname, lastname)
	else:
		name = 'Ms. %s %s' % (firstname, lastname)
	records.append((int(age), name))
records.sort(key=lambda item: item[0])
for (age, name) in records:
	print(name)


ar = []
n = int(input())
for i in range(0, n):
	str_ar = input().strip().split()
	user_name = str_ar[0] + ' ' + str_ar[1]
	user_age = int(str_ar[2])
	user_sex = str_ar[3]
	user_new_name = ''
	if user_sex == 'M':
		user_new_name = 'Mr. ' + user_name
	else:
		user_new_name = 'Ms. ' + user_name
	ar.append([user_new_name, user_age])
l = sorted(ar, key=lambda tup: tup[1])
for i in range(0, n):
	print(l[i][0])


from operator import itemgetter
test = int(input())
b = []
for r in range(0, test):
	a = input().split()
	b.append(a)
b.sort(key=itemgetter(2))
for r in b:
	if r[3] == 'M':
		print('Mr.', r[0], r[1])
	else:
		print('Ms.', r[0], r[1])


n = int(input())
allData = []
for i in range(n):
	data = input()
	data = data.split(' ')
	allData.append(data)
allData = sorted(allData, key=lambda lst: lst[2])
for lst in allData:
	if lst[3] == 'F':
		print('Ms. %s %s' % (lst[0], lst[1]))
	else:
		print('Mr. %s %s' % (lst[0], lst[1]))


from operator import itemgetter
n = int(input())
Dir = [0 for i in range(n)]
for i in range(n):
	info = input()
	info = info.split()
	First = info[0]
	Last = info[1]
	Age = int(info[2])
	Gender = info[3]
	if Gender == 'M':
		FullName = 'Mr.'
	else:
		FullName = 'Ms.'
	FullName = FullName + ' ' + First + ' ' + Last
	Dir[i] = [FullName, Age]
Dir.sort(key=itemgetter(1))
for N in Dir:
	print(N[0])


def tweak(f):

	def moddedf(person):
		person = ['Mr.' if person[3] == 'M' else 'Ms.', person[0], person[1]]
		f(person)
	return moddedf

@tweak
def showPerson(person):
	print(' '.join(person))
people = [input().split() for i in range(int(input()))]
people.sort(key=lambda person: int(person[2]))
for person in people:
	showPerson(person)


from operator import itemgetter
t = int(input())
a = [[0 for i in range(t)] for j in range(t)]
for i in range(t):
	ip = input().split(' ')
	try:
		a[i][0] = ip[0] + ' ' + ip[1]
		a[i][1] = int(ip[2])
		a[i][2] = ip[3]
	except:
		pop = -1
try:
	a = sorted(a, key=itemgetter(1))
except:
	bib = 0
for j in range(len(a)):
	try:
		if a[j][2] == 'M':
			print('Mr. ' + a[j][0])
		else:
			print('Ms. ' + a[j][0])
	except:
		print('Ms. ' + a[j][0])


lis = []
for i in range(int(input())):
	lis.append(input().split())
from operator import itemgetter
lis.sort(key=itemgetter(2))
for x in lis:
	if x[3] == 'M':
		print('Mr. ' + x[0] + ' ' + x[1])
	else:
		print('Ms. ' + x[0] + ' ' + x[1])


from operator import itemgetter
nested_list = []
n = int(input())
for i in range(n):
	input_list = []
	(fname, lname, age, sex) = input().split()
	age = int(age)
	input_list.append(fname)
	input_list.append(lname)
	input_list.append(age)
	input_list.append(sex)
	nested_list.append(input_list)
nested_list.sort(key=itemgetter(2))
for j in range(n):
	if nested_list[j][3] == 'M':
		print('Mr. ', end='')
	elif nested_list[j][3] == 'F':
		print('Ms. ', end='')
	f = nested_list[j][0]
	l = nested_list[j][1]
	print('%s' % f, end='')
	print(' ', end='')
	print('%s' % l)


x = int(input())
contact = []
for i in range(x):
	contact.append(input())
cont = []
for f in contact:
	nest = f.split()
	cont.append(nest)
from operator import itemgetter
for (a, b, c, d) in sorted(cont, key=itemgetter(2)):
	if d == 'M':
		print('Mr. ' + a + ' ' + b)
	else:
		print('Ms. ' + a + ' ' + b)


from operator import itemgetter
num = int(input())
record = []
for i in range(num):
	line = input().split(' ')
	record.append(line)
record.sort(key=itemgetter(2))
for j in record:
	if j[3] == 'M':
		print('Mr. ' + j[0] + ' ' + j[1])
	else:
		print('Ms. ' + j[0] + ' ' + j[1])


from operator import itemgetter
n = int(input())
r = []
for i in range(n):
	r.append(list(input().split()))
for p in sorted(r, key=itemgetter(2)):
	print('Mr.' if p[3] == 'M' else 'Ms.', p[0], p[1])


from operator import itemgetter
l = []
for i in range(int(input())):
	l.append(input().split())
l.sort(key=itemgetter(2))
for r in l:
	print('Mr.' if r[3] == 'M' else 'Ms.', r[0], r[1])


data = []
for _ in range(int(input())):
	data.append(input().split())
data.sort(key=lambda x: x[2])
for el in data:
	(name, surname, _, sex) = el
	pre = 'Ms.' if sex == 'F' else 'Mr.'
	print('%s %s %s' % (pre, name, surname))


n = input()
n = int(n)
from operator import itemgetter
s1 = []
while n > 0:
	s = input()
	s = s.split(' ')
	s[2] = int(s[2])
	s1 = s1 + [s]
	n -= 1
s1.sort(key=itemgetter(2))
for i in s1:
	if i[3] == 'M':
		print('Mr. ' + i[0] + ' ' + i[1])
	else:
		print('Ms. ' + i[0] + ' ' + i[1])


from operator import itemgetter
from itertools import groupby
N = int(input())
lst = list()
for i in range(N):
	a = input()
	lst.append(a.split())
lst.sort(key=itemgetter(2))
for (elt, items) in groupby(lst, itemgetter(2)):
	for i in items:
		if i[3] == 'M':
			print('Mr.', i[0], i[1])
		else:
			print('Ms.', i[0], i[1])


import operator
s = []
for i in range(int(input())):
	(first, last, age, sex) = input().split()
	name = '{}. {} {}'.format('Mr' if sex == 'M' else 'Ms', first, last)
	s.append((int(age), name))
s.sort(key=operator.itemgetter(0))
for item in s:
	print(item[1])


test = int(input())
dir = []
for case in range(test):
	name = input()
	lst = name.split()
	dir.append(lst)
dir.sort(key=lambda x: x[2])
for lst in dir:
	if lst[3] == 'M':
		print('Mr. ' + lst[0] + ' ' + lst[1])
	else:
		print('Ms. ' + lst[0] + ' ' + lst[1])


from operator import itemgetter

def as_int(func):

	def inner(*arg):
		ret = func(*arg)
		return int(ret)
	return inner

def format_name(fname, lname, age, sex):
	return '{} {} {}'.format('Ms.' if sex == 'F' else 'Mr.', fname, lname)
n = int(input())
people = sorted([input().split() for i in range(n)], key=as_int(itemgetter(2)))
for name in map(lambda l: format_name(*l), people):
	print(name)


def fn(item):
	if item[3] == 'M':
		return 'Mr. ' + item[0] + ' ' + item[1]
	else:
		return 'Ms. ' + item[0] + ' ' + item[1]
n = int(input())
names = []
for i in range(n):
	names.append(input().split())
names = sorted(names, key=lambda x: int(x[2]))
for item in names:
	print(fn(item))


from operator import itemgetter
N = int(input())
lists = list()
for i in range(N):
	lists.append(input().split())
lists.sort(key=itemgetter(2))
for item in lists:
	if item[3] == 'M':
		print('Mr.' + ' ' + item[0] + ' ' + item[1])
	else:
		print('Ms.' + ' ' + item[0] + ' ' + item[1])


n = int(input())
people = [input().split() for _ in range(n)]
people = sorted(people, key=lambda x: int(x[2]))

def printPerson(p):
	print('Ms.' if p[3] == 'F' else 'Mr.', p[0], p[1])
for person in people:
	printPerson(person)


N = int(input())
lis = []
for i in range(N):
	temp = input().split()
	lis.append({'first': temp[0], 'last': temp[1], 'age': int(temp[2]), 'sex': temp[3]})
lis.sort(key=lambda d: d['age'])
for i in lis:
	if i['sex'] == 'M':
		print('Mr.', end=' ')
	else:
		print('Ms.', end=' ')
	print(i['first'], i['last'], sep=' ')


from operator import itemgetter
n = int(input())
people = []
for i in range(n):
	line = input()
	person = line.split()
	person.append(i)
	people.append(person)
people.sort(key=itemgetter(2, 4))
for person in people:
	title = 'Mr.' if person[3] == 'M' else 'Ms.'
	print(title, end=' ')
	print(person[0], end=' ')
	print(person[1])


from operator import itemgetter
N = int(input())
name_book = []
for _ in range(N):
	(firstname, lastname, age, sex) = input().split()
	age = int(age)
	name = ''
	assert sex == 'M' or sex == 'F', 'undefined sex'
	if sex == 'M':
		name = 'Mr. '
	elif sex == 'F':
		name = 'Ms. '
	name += firstname + ' ' + lastname
	name_book += [(name, age)]
for (name, age) in sorted(name_book, key=itemgetter(1)):
	print(name)


import sys
import re
personre = re.compile('^(?P<name>\\w+ \\w+) (?P<age>\\d+) (?P<sex>M|F)')
title = {'M': 'Mr.', 'F': 'Ms.'}

def standardize_people(func):

	def f(people):
		people = [personre.match(person) for person in people]
		return func(people)
	return f

@standardize_people
def sort_people(people):
	return sorted(people, key=lambda x: int(x.group('age')))
(_, people) = (input(), sys.stdin.read().strip().split('\n'))
for person in sort_people(people):
	print('{} {}'.format(title[person.group('sex')], person.group('name')))


from operator import itemgetter
max = int(input())
lis = []
for i in range(0, max):
	tempstring = input()
	templist = tempstring.split()
	lis.append(templist)
lis.sort(key=itemgetter(2))
for (firstname, lastname, age, sex) in lis:
	if sex == 'M':
		print('Mr. ' + firstname + ' ' + lastname)
	else:
		print('Ms. ' + firstname + ' ' + lastname)


directory = sorted([input().split() for _ in range(int(input()))], key=lambda x: x[2])
print('\n'.join(('%s %s %s' % ({'M': 'Mr.', 'F': 'Ms.'}[x[3]], x[0], x[1]) for x in directory)))


from operator import itemgetter
n = int(input())
persons = list()
for i in range(n):
	p = input().split(' ')
	p[2] = int(p[2])
	persons.append(p)
persons = sorted(persons, key=itemgetter(2))
for p in persons:
	print(('Mr.' if p[3] == 'M' else 'Ms.') + ' ' + p[0] + ' ' + p[1])


N = int(input())
P = [input().split() for _ in range(N)]

def getKey(p):
	return int(p[2])
for p in sorted(P, key=getKey):
	print(('Ms. ' if p[3] == 'F' else 'Mr. ') + p[0] + ' ' + p[1])


from operator import itemgetter
from itertools import groupby
N = int(input())
peeps = list()
for n in range(N):
	person = input()
	peeps.append(person.split())
peeps.sort(key=itemgetter(2))
for (x, peop) in groupby(peeps, itemgetter(2)):
	for p in peop:
		if p[3] == 'M':
			print('Mr. ' + p[0] + ' ' + p[1])
		else:
			print('Ms. ' + p[0] + ' ' + p[1])


n = int(input())
dct = {}
for i in range(n):
	(fn, sn, age, sex) = input().split()
	age = int(age)
	if sex == 'M':
		name = 'Mr. ' + fn + ' ' + sn
	else:
		name = 'Ms. ' + fn + ' ' + sn
	if age in dct:
		dct[age].append(name)
	else:
		dct[age] = [name]
for age in sorted(dct.keys()):
	for person in dct[age]:
		print(person)


number = input()
records = {}
for i in range(int(number)):
	d = input()
	details = d.split()
	records[i] = details
	records[i][2] = int(records[i][2])
records = sorted(records.items(), key=lambda x: x[1][2])
for i in range(len(records)):
	if records[i][1][3] == 'M':
		print('Mr. ' + records[i][1][0] + ' ' + records[i][1][1])
	elif records[i][1][3] == 'F':
		print('Ms. ' + records[i][1][0] + ' ' + records[i][1][1])


listNames = []
listAges = []
n = int(input())
for i in range(n):
	details = input().split(' ')
	if details[3] == 'M':
		details[0] = 'Mr. ' + details[0]
	else:
		details[0] = 'Ms. ' + details[0]
	full = details[0] + ' ' + details[1]
	age = int(details[2])
	c = 0
	for a in listAges:
		if age < a:
			listAges.insert(c, age)
			listNames.insert(c, full)
			break
		c += 1
	if c == len(listAges):
		listAges.append(age)
		listNames.append(full)
for el in listNames:
	print(el)


n = int(input())
list = []
i = n
while i != 0:
	str = input()
	sub_list = str.split()
	list.append(sub_list)
	i = i - 1
from operator import itemgetter
list.sort(key=itemgetter(2))
i = 0
while i < n:
	if list[i][3] == 'M':
		print('Mr. {0} {1}'.format(list[i][0], list[i][1]))
	else:
		print('Ms. {0} {1}'.format(list[i][0], list[i][1]))
	i = i + 1


from operator import itemgetter
input_len = int(input())
input_buffer = [input().split() + [i] for i in range(input_len)]
output_buffer = sorted(input_buffer, key=itemgetter(2, 4))
for person in output_buffer:
	if person[3] == 'M':
		gender_prefix = 'Mr'
	else:
		gender_prefix = 'Ms'
	print('{prefix}. {f_name} {l_name}'.format(prefix=gender_prefix, f_name=person[0], l_name=person[1]))


N = int(input())
lst = []
for x in range(0, N):
	lst.append(input().split())
from operator import itemgetter
lst.sort(key=itemgetter(2))
for x in lst:
	if x[3] == 'M':
		print('Mr. ' + x[0] + ' ' + x[1])
	else:
		print('Ms. ' + x[0] + ' ' + x[1])


d = {}
for i in range(int(input())):
	(name, surname, age, sex) = input().strip().split()
	if age in d.keys():
		d[age].append((name, surname, sex))
	else:
		d[age] = [(name, surname, sex)]
for key in sorted(d.keys()):
	peoples = d[key]
	for (name, surname, age) in peoples:
		s = ''
		if age == 'M':
			s += 'Mr. '
		else:
			s += 'Ms. '
		s += name + ' ' + surname
		print(s)


from operator import itemgetter
N = int(input())
people = []
for n in range(N):
	a = input().split()
	people.append(a)
for a in sorted(people, key=lambda x: int(x[2])):
	if a[3] == 'M':
		print('Mr. ', end='')
	else:
		print('Ms. ', end='')
	print(a[0], a[1])


from operator import itemgetter
from itertools import groupby

def nice_log_for_nice_people(func):

	def inner(nice_people):
		gender = {'M': 'Mr. ', 'F': 'Ms. '}
		return [gender[x[2]] + x[0] for x in func(nice_people)]
	return inner

@nice_log_for_nice_people
def sort_people_by_age_stable(people):
	people.sort(key=itemgetter(1))
	return people
n = int(input())
people = []
for _ in range(n):
	stuff = input().split()
	name = stuff[0] + ' ' + stuff[1]
	age = int(stuff[2])
	gender = stuff[3]
	people.append([name, age, gender])
print('\n'.join(sort_people_by_age_stable(people)))


from operator import itemgetter
N = int(input())
people = []
for n in range(N):
	a = input().split()
	a[2] = int(a[2])
	people.append(a)
people.sort(key=itemgetter(2))
for a in people:
	if a[3] == 'M':
		print('Mr. ', end='')
	else:
		print('Ms. ', end='')
	print(a[0], a[1])


lines = []
for i in range(int(input())):
	lines.append(input().split())

def getKey(item):
	return item[2]
for i in sorted(lines, key=getKey):
	sex = 'Mr. ' if i[3] == 'M' else 'Ms. '
	print(sex + i[0] + ' ' + i[1])


from operator import itemgetter
t = int(input())
l = []
for i in range(t):
	temp = input().split()
	l.append([temp[0] + ' ' + temp[1], int(temp[2]), temp[3]])
l.sort(key=itemgetter(1))
for i in l:
	if i[2] == 'M':
		print('Mr. ' + i[0])
	else:
		print('Ms. ' + i[0])


(f, l, a, g) = ([], [], [], [])
for i in range(int(input())):
	b = input().split()
	f.append(b[0])
	l.append(b[1])
	a.append(int(b[2]))
	g.append(b[3])
for i in range(len(a)):
	m = min(a)
	p = 0
	G = ''
	for s in range(len(a)):
		if a[s] == m:
			a[s] = 1000000000
			p = s
			break
	if g[p] == 'M':
		G = 'Mr. '
	else:
		G = 'Ms. '
	print(G + f[p] + ' ' + l[p])


N = int(input())
info = []
for i in range(N):
	info.append(input())
	info[i] = info[i].split()
info.sort(key=lambda pair: pair[2])
for i in info:
	if i[3] == 'F':
		print('Ms.', i[0], i[1])
	else:
		print('Mr.', i[0], i[1])


import operator
xs = [input().split() for _ in range(int(input()))]
for x in xs:
	x[2] = int(x[2])
xs.sort(key=operator.itemgetter(2))
for x in xs:
	print(' '.join(['Mr.' if x[3] == 'M' else 'Ms.', x[0], x[1]]))


N = input()
s = [input().split(' ') for x in range(int(N))]
s.sort(key=lambda x: x[2])
l = [print('Mr.', x[0], x[1], sep=' ') if x[3] == 'M' else print('Ms.', x[0], x[1], sep=' ') for x in s]


from operator import itemgetter
n = int(input())
dic = []
for x in range(n):
	dic.append(input().split())
dic.sort(key=itemgetter(2))
for x in dic:
	if x[3] == 'M':
		print('Mr.', x[0], x[1])
	else:
		print('Ms.', x[0], x[1])


from sys import argv
import collections
var = int(input())
map = {}
for num in range(0, var):
	info = input().split(' ')
	fname = info[0]
	lname = info[1]
	age = info[2]
	sex = info[3]
	if int(age) in map:
		map[int(age)].append([fname, lname, sex])
	else:
		map[int(age)] = [[fname, lname, sex]]
sortedmap = collections.OrderedDict(sorted(map.items()))
for (k, v) in sortedmap.items():
	for info in v:
		if info[2] == 'M':
			print('Mr.', info[0], info[1])
		else:
			print('Ms.', info[0], info[1])


import sys
if sys.version_info[0] >= 3:
	raw_input = input
z = []
for i in range(int(raw_input())):
	a = raw_input().split()
	x = 'Mr.' if a.pop() == 'M' else 'Ms.'
	n = int(a.pop())
	z.append([n, i, ' '.join([x] + a)])
z.sort()
for e in z:
	print(e[2])


n = int(input())
dict1 = {}
for i in range(n):
	s1 = input()
	s2 = s1.split(' ')
	if int(s2[2]) in dict1:
		if s2[3] == 'M':
			dict1[int(s2[2])].append('Mr.' + ' ' + s2[0] + ' ' + s2[1])
		else:
			dict1[int(s2[2])].append('Ms.' + ' ' + s2[0] + ' ' + s2[1])
	elif s2[3] == 'M':
		dict1[int(s2[2])] = ['Mr.' + ' ' + s2[0] + ' ' + s2[1]]
	else:
		dict1[int(s2[2])] = ['Ms.' + ' ' + s2[0] + ' ' + s2[1]]
for key in sorted(dict1):
	for z in dict1[key]:
		print(z)


from operator import itemgetter
t = int(input())
lst = []
while t > 0:
	n = input()
	m = n.split(' ')
	name = m[0] + ' ' + m[1]
	age = int(m[2])
	sex = m[3]
	l = [name, age, sex]
	lst.append(l)
	lst.sort(key=itemgetter(1))
	t = t - 1
for i in lst:
	if i[2] == 'M':
		print('Mr. ' + str(i[0]))
	else:
		print('Ms. ' + str(i[0]))


n = int(input())
x = []
for i in range(n):
	a = input().split()
	if a[3] == 'M':
		x.append(['Mr.', a])
	elif a[3] == 'F':
		x.append(['Ms.', a])
x = sorted(x, key=lambda f: f[1][2])
for i in x:
	print(i[0], i[1][0], i[1][1])


from operator import itemgetter
n = int(input())
lisInput = ''
lis = []
for i in range(0, n):
	lisInput = str(input())
	lis.append(lisInput.split(' '))
lis.sort(key=itemgetter(2))
for i in range(0, n):
	lisInput = lis[i]
	if lisInput[3] == 'M':
		print('Mr. ' + lisInput[0] + ' ' + lisInput[1])
	else:
		print('Ms. ' + lisInput[0] + ' ' + lisInput[1])


n = int(input())
l = []
for i in range(n):
	s = input().split()
	if s[3] == 'M':
		s[0] = 'Mr. ' + s[0]
	else:
		s[0] = 'Ms. ' + s[0]
	l.append([int(s[2]), s[0] + ' ' + s[1]])
l.sort(key=lambda x: x[0])
for i in l:
	print(i[1])


n = int(input())
rows = []
for i in range(n):
	fields = input().split()
	rows.append({'name': fields[0], 'surname': fields[1], 'age': fields[2], 'prefix': 'Mr.' if fields[3] == 'M' else 'Ms.'})
rows = sorted(rows, key=lambda r: r['age'])
for row in rows:
	print('%(prefix)s %(name)s %(surname)s' % row)


from operator import itemgetter
n = int(input())
l = []
for i in range(n):
	s = input().strip().split()
	l.append(s)
l.sort(key=itemgetter(2))
for i in l:
	if i[3] == 'M':
		print('Mr. ' + i[0] + ' ' + i[1])
	else:
		print('Ms. ' + i[0] + ' ' + i[1])


from operator import itemgetter

def format_person(person):
	if person[3] == 'M':
		prefix = 'Mr. '
	else:
		prefix = 'Ms. '
	return prefix + person[0] + ' ' + person[1]
people = []
for i in range(int(input())):
	people.append(tuple(input().split()))
people.sort(key=itemgetter(2))
for person in people:
	print(format_person(person))


from operator import itemgetter

def wrapper(foo):

	def standard(l):
		l = foo(l)
		list_temp = []
		for k in l:
			if k[-1] == 'M':
				temp = 'Mr. %s %s' % (k[0], k[1])
				list_temp.append(temp)
			else:
				temp = 'Ms. %s %s' % (k[0], k[1])
				list_temp.append(temp)
		return list_temp
	return standard

@wrapper
def sort_number(l):
	l.sort(key=itemgetter(2))
	return l
N = int(input())
l = []
for i in range(0, N):
	temp = input().split()
	l.append(temp)
for k in sort_number(l):
	print(k)


from operator import itemgetter

def decorator(l):

	def addSalutation():
		if l[3] == 'M':
			return 'Mr. ' + l[0] + ' ' + l[1]
		else:
			return 'Ms. ' + l[0] + ' ' + l[1]
	return addSalutation
N = int(input())
biglist = []
while N > 0:
	lis = [x for x in input().split()]
	biglist.append(lis)
	N -= 1
biglist.sort(key=itemgetter(2))
for li in biglist:
	newlist = decorator(li)
	print(newlist())


from operator import itemgetter

def main():
	numpeople = int(input())
	people = []
	for counter in range(numpeople):
		persontemp = input().split()
		personname = ''
		if persontemp[-1] == 'M':
			personname = 'Mr. ' + persontemp[0] + ' ' + persontemp[1]
		elif persontemp[-1] == 'F':
			personname = 'Ms. ' + persontemp[0] + ' ' + persontemp[1]
		people.append([personname, int(persontemp[2]), persontemp[3]])
	people.sort(key=itemgetter(1))
	for person in people:
		print(person[0])
main()


from operator import itemgetter
i = int(input())
l = []
for j in range(i):
	det = input()
	l.append(det.split())
l.sort(key=itemgetter(2))
for i in l:
	if i[3] == 'M':
		print('Mr' + '.' + ' ' + i[0] + ' ' + i[1])
	else:
		print('Ms' + '.' + ' ' + i[0] + ' ' + i[1])


class Person:

	def __init__(self, name, familyName, age, gender):
		self.name = name
		self.familyName = familyName
		self.fullName = '{0} {1}'.format(name, familyName)
		self.age = age
		self.gender = gender

	def __str__(self):
		mr = 'Mr.'
		if self.gender == 'F':
			mr = 'Ms.'
		return '{0} {1} {2}'.format(mr, self.name, self.familyName)

	def __repr__(self):
		return self.__str__()
persons = []
for i in range(int(input())):
	lst = input().split()
	persons.append(Person(lst[0], lst[1], int(lst[2]), lst[3]))
[print(x) for x in sorted(persons, key=lambda y: y.age)]


import operator
t = int(input())
dr = []
for i in range(0, t):
	j = list(input().split())
	dr.append(j)
byage = operator.itemgetter(2)
dr = sorted(dr, key=byage)
for human in dr:
	if human[3] == 'M':
		s = 'Mr.'
	else:
		s = 'Ms.'
	print(s, human[0], human[1])


from operator import itemgetter

def concat_list_items_as_text(l):
	phrase = ''
	for word in l:
		phrase += word + ' '
	phrase.strip()
	return phrase
num_entries = int(input())
entries = []
for idx in range(num_entries):
	raw_entry = input().split()
	name = concat_list_items_as_text(raw_entry[:-2])
	entry = [name]
	entry.append(int(raw_entry[-2]))
	entry.append(raw_entry[-1].upper())
	entries.append(entry)
entries.sort(key=itemgetter(1))
for entry in entries:
	if entry[-1] == 'F':
		salutation = 'Ms. '
	else:
		salutation = 'Mr. '
	print('%s%s' % (salutation, entry[0]))


x = []
for i in range(int(input())):
	x.append(input().split())
	from operator import itemgetter
x.sort(key=itemgetter(2))
for i in x:
	if i[3] == 'M':
		print('Mr. {} {}'.format(i[0], i[1]))
	else:
		print('Ms. {} {}'.format(i[0], i[1]))


import re
ppl = []
for x in range(int(input())):
	r = re.match('^(\\w+ \\w+) (\\d+) ([MF])', input())
	if r:
		ppl.append(r)
ppl = sorted(ppl, key=lambda x: int(x.group(2)))
for p in ppl:
	print(('Mr. ' if p.group(3) is 'M' else 'Ms. ') + p.group(1))


from operator import *
n = int(input())
s = ''
a = [[]] * n
x = ['', 1]
for i in range(n):
	s = input()
	s = s.split()
	if s[3] == 'M':
		x = ['Mr. ', s[2]]
	else:
		x = ['Ms. ', s[2]]
	x[0] += s[0] + ' ' + s[1]
	a[i] = x
a.sort(key=itemgetter(1))
for i in range(n):
	print(a[i][0])


def orderInput(func, info):
	info.sort(key=lambda person: (person[2], person[4]))
	func(info)

def printUsers(info):
	print('\n'.join((('Mr. ' if x[3] == 'M' else 'Ms. ') + x[0] + ' ' + x[1] for x in info)))
info = [[x for x in input().split()] + [input_order] for input_order in range(int(input()))]
orderInput(printUsers, info)


from operator import itemgetter
n = int(input())
details = []
for x in range(0, n):
	temp = input()
	det = temp.split()
	details.append(det)
details.sort(key=itemgetter(2))
for d in details:
	if d[3] == 'M':
		print('Mr. %s %s' % (d[0], d[1]))
	else:
		print('Ms. %s %s' % (d[0], d[1]))


n = int(input())
from operator import itemgetter
people = []
for i in range(n):
	people.append(input().split())
people.sort(key=itemgetter(2))

def p(item):
	print('%s %s %s' % ('Mr.' if item[3] == 'M' else 'Ms.', item[0], item[1]))
[p(item) for item in people]


a = []
T = int(input())
for _ in range(T):
	x = input().strip().split()
	x[2] = int(x[2])
	a.append(x)
a.sort(key=lambda x: x[2])
for x in a:
	if x[3] == 'M':
		print('Mr.', x[0], x[1])
	else:
		print('Ms.', x[0], x[1])


N = int(input())
a = []
for i in range(0, N):
	s = input()
	s = s.split()
	s[2] = int(s[2])
	a.append(s)
a.sort(key=lambda x: x[2])
for i in range(0, N):
	if a[i][3] == 'F':
		print('Ms. ' + a[i][0] + ' ' + a[i][1])
	elif a[i][3] == 'M':
		print('Mr. ' + a[i][0] + ' ' + a[i][1])


from operator import itemgetter
n = int(input())
newlist = []
for i in range(0, n):
	y = input().split(' ')
	newlist.append(y)
newlist.sort(key=itemgetter(2))
for i in range(0, len(newlist)):
	if newlist[i][-1] == 'M':
		print('Mr. ' + str(newlist[i][0]) + ' ' + str(newlist[i][1]))
	if newlist[i][-1] == 'F':
		print('Ms. ' + str(newlist[i][0]) + ' ' + str(newlist[i][1]))


from datetime import *
N = int(input())
Book = []
for i in range(N):
	info = input().split()
	people = {}
	people['FN'] = info[0]
	people['LN'] = info[1]
	people['age'] = int(info[2])
	people['gender'] = info[3]
	people['join'] = datetime.now()
	Book.append(people)
s1 = sorted(Book, key=lambda people: people['join'])
s2 = sorted(s1, key=lambda people: people['age'])
for people in s2:
	title = ''
	if people['gender'] == 'M':
		title = 'Mr.'
	else:
		title = 'Ms.'
	print('{0} {1} {2}'.format(title, people['FN'], people['LN']))


from operator import itemgetter
a = sorted((input().split() for i in range(int(input()))), key=itemgetter(2))
print(*('{} {} {}'.format('Mr.' if s[3] == 'M' else 'Ms.', s[0], s[1]) for s in a), sep='\n')