import strutils, sequtils, algorithm, tables proc getStr: auto = stdin.readLine var dic = initCountTable[string]() let N = getStr().parseInt for i in 0 ..< N: let No, S = getStr().split.map parseInt Tags = getStr().split for str in Tags: if str in dic: dic[str] += S[1] else: dic[str] = S[1] var i = 0 dic2 = newSeq[tuple[n: string, t: int]](dic.len) for k, v in dic: dic2[i].n = k dic2[i].t = v i += 1 for i, d in sortedByIt(dic2, (-it.t, it.n)): if i >= 10: break echo d.n, " ", d.t