The 4.0 and 5.0 ah (amp hour) rating doesn't matter at all, it's nothing more than an indication of how many amps can be drawn for one hour from the battery in ideal/normal conditions, the larger the ah rating, the longer the tool can be powered. So a 4ah battery can deliver 4a for one hour, or 2a for two hours, 1a for four hours, etc. If a 4ah battery lasts you for 8 hours use before needing a charge, then you know that tool pulls 0.5a of current in use. Roughly speaking that is because load, heat and a number of other factors can affect the amount of load drawn.
All that matters is if the voltage is correct. In this case 18v, so all good.
If you wanted you could even use the 8ah batteries for max runtime.