#!/usr/bin/python3
# Encoding: utf8

import sys
import re
import math
from collections import defaultdict

th = sys.argv[1]
th = int(th)

w = defaultdict(dict)

for line in sys.stdin:  
    line = line.strip()  ## chomp $line
    #print (line)
    line = line.split('\t')
    if len(line) >= 3:
        word1 = line[0]
        word2 = line[1]
        simil =  float (line[2])

        w[word1][word2] = simil


#for w1, second in sorted(w.items() ):
for w1, second in w.items():
    i=1
    for w2 in sorted(second,key=second.get,reverse=True):
        if i <= th:
        #    print >> sys.stderr, "threshold:", th, i
            print ('%s\t%s\t%.6f' % (w1, w2, w[w1][w2]))
        i += 1
