n = int(raw_input()) s = [raw_input.split() for _ in xrange(n)] s.sort(key=lambda x:x[1]) s.sort() for a, b in s: print(a, b)