"Did it hurt when you touched the wire?", the doctor asked.
Let's say that we want to put the above sentence in reported speech.
Would it be
The doctor asked if it had hurt when I touched the wire.
or
The doctor asked if it had hurt when I had touched the wire.
I always find such sentences quite confusing.