def build_merged_index(merged_modules, quadrilaterals):
    """Creates a mapping (dict) of the form {module_id: merged_module_id}.
    Here, `module_id` is the original module ID as in the quadrilaterals dict. The
    `merged_module_id` corresponds to the module ID of the first module the module
    is merged with. If the module is not merged with another module,
    `merged_module_id` equals `module_id`."""
    module_ids = set([module_id for module_id, _, _ in quadrilaterals.keys()])
    if merged_modules is None:
        merged_index = {module_id: module_id for module_id in module_ids}
    else:
        merged_index = {}
        for module_id in module_ids:    
            merged_id = module_id
            for modules in merged_modules:
                if module_id in modules:
                    merged_id = modules[0]            
            merged_index[module_id] = merged_id
    return merged_index