import java.util.Scanner object Main extends App { val sc = new Scanner(System.in) val n = sc.nextInt var mp = Map.empty[String, Int] for(_ <- 0 until n) { val _ = sc.nextInt val m = sc.nextInt val s = sc.nextInt for(t <- Array.fill(m)(sc.next)) { val cs = mp.getOrElse(t, 0) mp += t -> (cs + s) } } val ta = mp.map(elem => Array(elem._1, elem._2)).toSeq val arr = ta.sortWith((t1, t2) => { if(t1(1).toString.toInt > t2(1).toString.toInt) { true } else if(t1(1).toString.toInt == t2(1).toString.toInt && t2(1).toString >= t1(0).toString) { true } else { false } }) val limit = math.min(10, arr.length) for(i <- 0 until limit) { println(arr(i).mkString(" ")) } }