def tuple_replace(tup, *pairs):
    """Return a copy of a tuple with some elements replaced.

    :param tup: The tuple to be copied.
    :param pairs: Any number of (index, value) tuples where index is the index
        of the item to replace and value is the new value of the item.
    """
    tuple_list = list(tup)
    for index, value in pairs:
        tuple_list[index] = value
    return tuple(tuple_list)