def filter_user_interactions(user_item_ints, valid_items, min_user_ints=5, max_user_ints=10000):
    """
    Given a dict of user-item interactions, returns a filtered dict of user-item interactions that:
    1 - Only keep the items that are in 'valid_items'
    2 - Only keep the user whose amount of interactions is between min_user_ints and max_user_ints.

    :param user_item_ints: dict of uid: set of iids
    :param valid_items: set with iids of valid items that the users are allowed to interact with
    :param min_user_ints: minimum amount of user interactions to be included in the result dict
    :param max_user_ints: maximum amount of user interactions to be included in the result dict
    :return: filtered_user_item_ints according to the previous criteria
    """
    filtered_user_item_ints = {}
    for uid, ints in user_item_ints.items():
        new_ints = set([iid for iid in ints if iid in valid_items])

        if min_user_ints <= len(new_ints) <= max_user_ints:
            filtered_user_item_ints[uid] = new_ints
    return filtered_user_item_ints