import heapq


def heapmerge(*inputs):
    """Like heapq.merge(), merges multiple sorted inputs (any iterables) into a single sorted output, but provides more convenient API:
    each input is a pair of (iterable, label) and each yielded result is a pair of (item, label of the input) - so that it's known what input a given item originates from.
    Labels can be any objects (e.g., object that produced the input stream)."""
    def entries(iterable, label):
        for obj in iterable: yield (obj, label)
    iterables = [entries(*inp) for inp in inputs]
    return heapq.merge(*iterables)