Hi,
Could you please provide an explanation of what you are doing here? It is a bit confusing. As per my knowledge, if you have the CMasking_ID render element and you have selected "Masking by ID" to "Material ID," then the result you are getting is expected. If you apply one material, then you will see one color ID in all objects, and if you apply multiple materials, you will see multiple color IDs. I may have misunderstood what you are trying to show here. Maybe if you can provide a simple scene with repro steps and tell me the expected result and what result you are getting, which is incorrect, then I may be able to help you further on this.