STANFORD, Calif. (AP) The game up in Seattle that impacted Stanford the most had been going well all night. That didn't matter to the Cardinal until they took over their own contest during a dizzying three-touchdown stretch in the opening minutes of the fourth quarter.

K.J. Costello threw two of his career-high four touchdown passes to start that scoring spree and 20th-ranked Stanford rallied for a 38-20 victory over No. 9 Notre Dame on Saturday night.

The night couldn't have gone much better for the Cardinal (9-3, No. 21 CFP), who got the help they needed when Washington beat Washington State 41-14 in the Apple Cup to send Stanford to the Pac-12 title game. The Cardinal finished tied for first in the North with the Huskies but won the head-to-head matchup to take the tiebreaker.

''I was just so worried about this game,'' Costello said. ''At the end of the day, I couldn't care less about that. Now I'm stoked that we have that opportunity.''

It didn't seem that it would come after Stanford lost back-to-back games to Southern California and San Diego State in September to fall to 1-2. But the Cardinal have won eight of nine since then with a boost from the change at quarterback to Costello and now get a rematch against USC in the conference championship game.

''We sat here 1-2 with a lot of doubt,'' coach David Shaw said. ''Some of it was internal. We had to ask ourselves a lot of questions. We had to change some pieces around on our team. We talked about not worrying about anything else other than playing our style of football, blocking out the noise. ... We had to go back and be who we are, hit a groove.''

The one constant was Bryce Love, who capped a memorable regular season by running for 125 yards on a gimpy ankle.

But Costello was the star of the offense for a change by throwing for 176 yards and delivering the four scores.

The Fighting Irish (9-3, No. 8) had a meltdown in the fourth quarter with two turnovers in the opening minutes and were dealt a serious blow in their hopes of making a New Year's Six bowl game.

The turnaround started when Costello found Kaden Smith down the middle for a 19-yard score on third down that gave Stanford the lead for good. Brandon Wimbush then was intercepted by Curtis Robinson on the first play of the ensuing drive.

The Cardinal needed just three plays to turn that into another TD when Costello found Dalton Schultz in the corner of the end zone for a 12-yard score.

C.J. Sanders then fumbled the kickoff for Notre Dame and Cameron Scarlett put the game away with 3-yard run to make it 38-20 and complete the spree that took only 3:36.

''We turned a good game into a not so good game by turning the ball over,'' Irish coach Brian Kelly said.

THE TAKEAWAY

NOTRE DAME: The Irish offense didn't do much outside of two long touchdown passes by Wimbush: an 83-yarder to Kevin Stepherson and a 75-yarder to Equanimeous St. Brown on the opening play of the second half. Notre Dame had just one sustained scoring drive. The Irish got a field goal on a drive that lost 2 yards and also had a 15-play, 68-yard drive in the first half to set up a field goal by Justin Yoon.

STANFORD: The Cardinal showed they are a much more potent team when they have a downfield passing game to complement Love's big-play runs. Costello frequently stretched the field, including three long throws to Smith.

MISSED OPPORTUNITY

Late in the third quarter, Notre Dame used a punt that was downed at the 1, a three-and-out defensively and a 41-yard punt return by Chris Finke to start a drive at the Stanford 19 with the game tied. Two procedure penalties backed the Irish up and they were forced to settle for a field goal instead of turning the short field into a touchdown.

''Just felt like we left some points out there,'' Kelly said. ''I didn't feel like it was slipping away in that sense, but I felt like we left some points out there.''

NO REST

Shaw said he didn't consider taking Love out to rest his sore ankle, even when it became clear early in the night that Washington State was going to lose and the Cardinal would have to play the title game on a short week. Love carried 20 times before finally getting a break when Stanford took a big lead in the fourth.

''I didn't know the Huskies were in command,'' Shaw said. ''Only thing I knew what was happening on our field.''

UP NEXT

NOTRE DAME: Bowl game.

STANFORD: Play USC in Pac-12 title game at Levi's Stadium in Santa Clara on Friday.

---

More AP college football: collegefootball.ap.org and twitter.com/AP-Top25

Copyright 2017 by AP. Any commercial use or distribution without the express written consent of AP is strictly prohibited.