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

# INPUT: non-zero matrix with pmi
# ARGUMENT 1: threshold for word frequency
# ARGUMENT 2: threshold for number of relevant contexts (contexts with higher pmi values)
# OUTPUT: a non-zero matrix filtered by word frequency and relevant contexts

import sys
import re
import math
from collections import defaultdict

th1 = int(sys.argv[1])
th2 = int(sys.argv[2])

w = defaultdict(dict)
w_fr = defaultdict(int)

for line in sys.stdin:
    line = line.strip() 
    line = line.split()
    if len(line) >= 3:
        word = line[0]
        context = line[1]
        fr =  float (line[2])

        w[word][context] = fr
        w_fr[word] += 1

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