You have already calculated the length of the individual segments correctly.
Answered here: How to scale and rotate instances based on segment length?
Also, in this context, we already had the question of how to find out the length of an object within a collection.
Answered here: How to get a field with individual object sizes inside a collection?
This question is primarily about how these individual mechanisms can be combined into one, and you can solve this as follows:

Here, I first capture an integer value at each of the points where objects are to be instantiated. This is important because we need exactly the same value when instantiating to be able to read the corresponding object and its dimensions from the collection.
You can also use any other values here, so it doesn't have to happen via the Random Value node. If the node Instance on Points receives no input as Instance Index, the instances are simply numbered (i.e.: $0,1,2,0,1,2,...$), which is why you can actually also use this number sequence to query the dimensions of the individual instances (Tip: Math Node Modulo).
To calculate the dimensions, I use in this example as I said the answer from your previous question, which looks (slightly modified) like this here:

You can of course evaluate the lengths of the other axes in a similar way. The details of this have been discussed in detail in here.
This length can now be transferred comfortably with exactly this previously created integer value during instantiation.

(Blender 3.1+)