inf = open("names.in", "r")
ouf = open("names.out", "w")

n = int(inf.readline())
a = []
v = dict()
for i in range(n):
    a.append(inf.readline().split())
    for x in a[-1]:
        if x in v:
            v[x] += 1
        else:
            v[x] = 1

b = []
for x in a:
    if v[x[0]] == 1:
        b.append(x)
    else:
        b.append([x[2], x[0], x[1]])

b.sort()
for x in b:
    print(" ".join(x), file = ouf)

inf.close()
ouf.close()
