This one is difficult. The first answer is correct. The second one would almost never be correct. Deny requires a direct challenge of some kind. That is, someone must directly make a request, statement, or other kind of challenge against the person who can then deny it. The challenge must always be clearly defined.
Testimony probably would not make such a direct challenge or statement. If the defendant's testimony directly accused the witness of a specific crime, the witness could then deny it. However such testimony would be very rare. Normally testimony consists of many different statements together.
Contradict is different because it does not require such specific things. It can make the challenge against something else by saying that something else is false. It does not require such clear definition.
For example, you say "I went to the store," and I say "I was at the store, but I did not see you there! You are lying."
I have contradicted you, but I have not denied you. You made no challenge against me. I did challenge you, however. I have accused you of lying. You could now deny that you are lying.
Note that if I had not said "You are lying," then you still could not deny my statement, because it is not clear what the challenge is. I did not make a direct, clear challenge.