def multiset_intersection(a, b):
    supply = {}
    for value in a:
        supply[value] = supply.get(value, 0) + 1
    output = []
    for value in b:
        if supply.get(value, 0) > 0:
            output.append(value)
            supply[value] -= 1
    return output
