God is an expert at bringing good out of bad. He could have kept Paul out of prison in Philippi but instead He let Paul go to prison and the jailer became a believer as a result.
God could have kept Jesus from the cross but He let Him go -- His own Son -- He let Him suffer and die. Did He bring any good out of that? I'd say He did!
The things we wish were most removed from our life are often the very things that God is using to shape us and make us into the believer of character He wants us to be. He wants to use that problem for good in our life. There's something more important than our pain. It's what we're learning from that pain. God is in control.
So what's the key? What's our response? Our response is to look past the pain. "This is the reason we never lose heart. These troubles which are temporary are winning for us a permanent, a glorious and solid reward out of proportion to our pain. So we don't look at the temporary situation. We look beyond to see the eternal benefits." (2 Corinthians 4:16-17
[You should] be exceedingly glad on this account, though now for a little while you may be distressed by trials and suffer temptations, So that [the genuineness] of your faith may be tested, [your faith] which is infinitely more precious than the perishable gold which is tested and purified by fire. [This proving of your faith is intended] to redound to [your] praise and glory and honor when Jesus Christ (the Messiah, the Anointed One) is revealed.
(1 Peter 1:6, 7 AMP)