let solve tagSeq = Seq.groupBy (fun (_, tag) -> tag) tagSeq |> Seq.map (fun (tag, mtSeq) -> (tag, Seq.sumBy (fun (m, _) -> m) mtSeq)) |> Seq.sortBy (fun (tag, sumM) -> (-sumM, tag)) |> Seq.mapi (fun i (tag, sumM) -> (i, tag, sumM)) |> Seq.takeWhile (fun (i, _, _) -> i < 10) |> Seq.iter (fun (_, tag, sumM) -> printfn "%s %d" tag sumM) let () = let n = stdin.ReadLine() |> int let tags = seq { for _ in 1..n do stdin.ReadLine() |> ignore let m = stdin.ReadLine() |> fun s -> s.Split() |> fun arr -> int arr.[1] let tagArr = stdin.ReadLine() |> fun s -> s.Split() yield (Array.map (fun tag -> (m, tag)) tagArr) } |> Seq.concat solve tags