MONTMELÓ, Spain (AP) — Charles Leclerc’s perfect weekend at the Spanish Grand Prix was almost ruined when his Ferrari spun to a stop late in qualifying.

Instead of panicking, Leclerc showed championship-caliber cool.

He got his car back to the garage for a new set of tires, then ripped off a last-gasp lap to bump reigning Formula One champion Max Verstappen for the pole in Saturday qualifying.

Leclerc shouted “that was good!” after his run in the closing moments of the third round of qualifying.

“I did a mistake on my first run, but in the end I made it stick,” Leclerc said. “Very happy with that lap and that I made it work with the only chance I had.”

Leclerc went 1 minute, 18.750 seconds around Circuit de Barcelona-Catalunya. He dropped Verstappen in a Red Bull to second by a margin of 0.323 seconds as Leclerc won his fourth pole of the season and 13th of his career.

Does he have more for Sunday?

History indicates the race should be Leclerc's to control: the pole-sitter in Spain has won 23 out of 31 times because the track has so few passing zones.

Verstappen, however, seemed unbothered by a mechanical problem on his Red Bull that prevented him from taking the pole away from Leclerc with Verstappen's own late run. And, Verstappen was third at the start in Miami two weeks ago behind the Ferrari's of Leclerc and Carlos Sainz Jr., and the Dutchman clawed past both of them for a second-consecutive victory.

The back-to-back wins shaved Leclerc's lead over Verstappen to 19 points in the standings headed into Sunday's race.

No matter the momentum that Verstappen currently has, he was one of the few drivers who failed to win from the pole in race history when he was beat last year by Lewis Hamilton.

Verstappen took a shot at bumping Leclerc but his DRS flap failed to open on his final run and ruined his chance. Now he’s banking on Red Bull’s good long runs and tire preservation skills to beat Leclerc come Sunday.

“It is a shame because I would have liked a last run,” said Verstappen, who got his first career win here in 2016. “Tomorrow will be very hot and this track is very hard to pass but hopefully it will kinder on our tires.”

Verstappen said Red Bull was looking into the malfunction. When his Red Bull hasn’t failed him, Verstappen has proven faster than Leclerc, whose two wins in Bahrain and Australia came after his top rival had to retire. Verstappen has won all three races that he has finished.

Meanwhile, Spain’s best chance at a win in its own grand prix took a decent turn when Sainz qualified third to put both Ferrari’s near the front.

Mercedes showed a significant turnaround from the struggles its had through the first five races of the season with its new car. George Russell qualified fourth and once again outran new teammate Hamilton, who has won the last five races here. The seven-time champion qualified sixth, with Red Bull driver Sergio Pérez sandwiched between the Mercedes drivers.

Leclerc, who was also fastest in all three practice sessions ahead of qualifying, snapped a streak of nine consecutive poles for Mercedes at the 4.6-kilometer (2.8-mile) circuit.

Qualifying for Sunday’s race was held on a warm, sunny day at the track near Barcelona. Although large parts of Spain are under a heatwave alert with May temperatures that rank among the hottest in two decades, it was a still bearable 34 C (93 F) during qualifying.

Ferrari is one of several teams that arrived in Spain with upgrades to their 2022 cars and was hopeful the team can stop Verstappen. But hot temperatures should make tire management key over the 66-lap race on a track notorious for being tough on rubber. Leclerc complained about it during Friday’s practice.

“I am in a strong position to start the race, but I have been struggling with my tires and with Max right behind, if we don’t manage them well we will lose that advantage,” Leclerc said.

Former champions Sebastian Vettel and local favorite Fernando Alonso both failed to advance out of the first qualifying round. When told by Aston Martin team radio that he was 16th, Vettel replied “you must be kidding.”

Alonso has not had a joyous homecoming so far. He qualified 17th for Alpine, accused the FIA of incompetence in race control, and was penalized during one of Friday’s practices.

Spain’s greatest driver has struggled this season and the two-time F1 champion has scored only two points, while Alpine teammate Esteban Ocon has scored 24.

Mick Schumacher clocked the 10th best time after his Haas needed a new gearbox because its back right wheel caught fire during the third practice session earlier on Saturday.

Over 100,000 fans are expected at the first Spanish GP with full spectators since 2019; the public was not permitted to attend in 2020 and only 1,000 fans were allowed inside in 2021 because of pandemic restrictions.

___

More AP auto racing: https://apnews.com/hub/auto-racing and https://twitter.com/AP_Sports