TURIN, Italy (AP)Andrey Rublev rallied to upset second-seeded Stefanos Tsitsipas and reach the final four of the ATP Finals on Friday.

Rublev won 3-6, 6-3, 6-2 to reach the semifinals for the first time. He will face third-seeded Casper Ruud.

Novak Djokovic plays Taylor Fritz of the United States in the other semifinal. The five-time champion will have to recover swiftly after a grueling dead rubber victory over Daniil Medvedev that lasted more than three hours.

”On the physical side, I’m not worried because . . . worry just depletes you of the vital energy you need,” Djokovic said. ”If something happens tomorrow in a good or bad way, it happens, and I have to deal with it then.

”I’m going to do everything I possibly can today with my physio, with myself, with my team in order to get the good rest, the good recovery . . . I’ve had many cases in my life before where I managed to recover very quickly. Hopefully that’s going to be the case again.”

Djokovic is aiming to equal Roger Federer’s record six titles at the elite, season-ending event. Djokovic’s last title was in 2015.

With a win and a loss apiece in the group, it was a winner-takes-all match for Rublev and Tsitsipas.

Rublev threw his racket down in frustration after losing a game on his serve early in the opening set. But he was all smiles at the end as he crouched down seemingly in disbelief when Tsitsipas hit a return into the net to hand him the match.

”I didn’t give up. I kept fighting and playing,” Rublev said. ”I lost my emotions a bit when I lost a stupid game at 40-0 in the first set.

”But then I just kept fighting. I thought I would have chances if I just gave my best. I managed to turn the match around and I am happy to be in the semis.”

After starting strong, Tsitsipas faded and made errors including a double fault in the penultimate game to give Rublev his second break of the final set and leave him serving for the match.

Earlier, Djokovic clearly pushed himself to the limits as he beat the already-eliminated Medvedev to maintain his perfect record in the tournament.

Though he was already through to the semifinals, Djokovic was playing for pride and ranking points, plus the chance to earn $4.7 million if he wins the trophy undefeated in Turin.

He was visibly shaking during changeovers in the third set as he battled to win 6-3, 6-7 (5), 7-6 (2).

Medvedev broke in the ninth game of the deciding set to leave him serving for the match but Djokovic broke straight back and went on take the tiebreaker.

”I don’t think that a limit exists,” Djokovic said. ”It’s really in your head. It’s really about perspective and approach and your perception of how you see things in that given moment.

”Of course, when you’re going through physical struggles, it affects the game, it affects how you feel mentally, it affects your body language. But I think the biggest battle, as I’ve said before, is always the inner battle.”

More AP tennis: https://apnews.com/hub/tennis and https://twitter.com/AP-Sports