KANSAS CITY, Mo. (AP) — MJ Melendez hit a two-run homer, Salvador Perez drove in the go-ahead run with a two-out double in the seventh inning, and the Kansas City Royals held off the rapidly fading Minnesota Twins 5-4 on Tuesday night to open their final homestand of the season.

Dylan Coleman (5-2) pitched a scoreless seventh for the Royals, then Scott Barlow cleaned up Jose Cuas’ mess by striking out Mark Contreras to strand runners on second and third in the eighth, before handling the ninth for his 22nd save.

“It was a tough game,” said Twins manager Rocco Baldelli, whose club has dropped five of six to begin an eight-game trip, including four of five to AL Central-leading Cleveland. “Things didn’t go the way we would have liked.”

The Royals trailed 3-0 in the second, pulled ahead in the fifth and were still tied 4-all when Melendez doubled off Twins reliever Michael Fulmer (5-6) in the seventh. Bobby Witt Jr. followed with a double, then took third when Melendez tried to score and was thrown out by Twins left fielder Jake Cave on a bang-bang play at the plate.

Perez came through on the very next pitch, though, dumping the go-ahead double down the right-field line.

“He just swells in those situations,” Royals manager Mike Matheny said. “You need that guy who says, ‘OK, I’m going to take care of this.'”

Kansas City, which was swept in three games at Target Field last week, had lost 12 of its last 14 to the Twins.

The Twins appeared to be continuing their mastery of the Royals this season, too, when they jumped ahead 3-0 in the second on Matt Wallner’s run-scoring double and a two-run single by Carlos Correa.

They nearly pushed another run across in the third, too, but Royals starter Zack Greinke struck out Gio Urshela and Cave, ending the threat while picking up his 1,000th strikeout in a Kansas City uniform.

“Just an incredible accomplishment,” Matheny said.

Melendez’s two-out homer in the third got Kansas City within 3-2, then Hunter Dozier’s shot leading off the fifth tied the game and ended a streak of 115 at-bats without a home run. Witt added a go-ahead RBI single later in the inning.

Cave answered for the Twins off reliever Amir Garrett to tie the game 4-all in the sixth.

Greinke allowed four runs on 10 hits with three strikeouts in 5 1/3 innings for Kansas City. Twins counterpart Dylan Bundy allowed four runs on four hits and a walk in 4 2/3 innings.

“Just poor execution with a couple of pitches,” Bundy said. “I’ve got to work deeper into games and I haven’t done that.”

SCARY MOMENT

Correa was thrown out at second base in the seventh inning, and the tag hit his lower right leg and left the Twins shortstop laying on the ground. He said afterward that “it just felt numb,” but Correa felt good enough to finish out the game.

GRAY GOES TO IL

Twins RHP Sonny Gray went on the injured list with a strained right hamstring that occurred during a start Sept. 2 against the White Sox. He threw just 44 pitches before a recurrence of the hamstring issues that he had in April.

“I don’t see him pitching if we’re not in it,” Baldelli said, ”but he’s going to stay mentally ready.”

RHP Trevor Megill was reinstated from the COVID-19 list and RHP Dereck Rodriguez was outrighted off the 40-man roster.

TRAINER’S ROOM

Royals RHP Taylor Clarke (oblique strain) threw about 15 pitches in a live session Tuesday. He’s scheduled to throw again on Friday in the hopes of rejoining the club before the season ends.

UP NEXT

Twins RHP Bailey Ober (1-2, 3.49 ERA) starts Wednesday night against Royals LHP Daniel Lynch (4-11, 5.15).