Evan Ferguson scored his first goal since November as the Republic of Ireland preserved their Nations League B status with a 4-2 aggregate victory over Bulgaria.