Beyondsoft International (Singapore) Pte. may receive repeated metric updates as key-value pairs. Given these pairs, combine values belonging to the same key and return the k keys with the largest totals.
Implement top_k_pairs(pairs, k). The input pairs is a list of two-element lists, where each pair contains a string key and an integer value. For every distinct key, calculate the sum of all its values. Return at most k two-element lists in the form [key, total].
Sort results by descending total. If two keys have equal totals, preserve the order in which their keys first appeared in pairs. Assume 1 <= k and that k may be larger than the number of distinct keys.
def top_k_pairs(pairs, k):