Ignore all collisions except with one object in script

Lets say I have 100 cubes instantiated. How can I get cube A and cube B to collide with each other, but none of the other 98 cubes (and repeat this behavior for all the cubes that exist)?
Is the only way to do this to attach a for loop on every single cube that loops through every other cube applying Physics.IgnoreCollision (except on the specified cube to collide with)?

If it’s just for a few different cubes, the best thing to do is use collision layers.
If that’s too limiting I guess it would be ignore collision or manual collision checks

Itll be a lot of different cubes. 400+

If you explain the game loop maybe I can help further, otherwise that’s all I can help with rn

Not really a game loop, but I’m trying to simulate a bunch of things. It’d be nice if I could just put them on top of each other, hence the ignoring collisions part.