Marcus Smith set up three tries, scored one and booted a penalty with the last kick of the game Friday to give Harlequins a 36-33 European Champions Cup victory in Cardiff.
Quins qualified for the knockout rounds after the 22-year-old England fly half scored the last 12 points of the match to save the erratic English champions in a wild game in which both teams scored five tries.
"I don't think we were at the races today," Smith told British broadcaster BT Sport. "I thought we stuck in there and were very resilient."
In the night's other game, also in Pool B, Gavin Coombes scored in the final minute to give Munster a 16-13 victory in Castres to climb to second after thee of their four games.
Harlequins took the lead when Smith's chip set up Louis Lynagh for a try after six minutes.
Cardiff started the match without a point after fielding a cobbled-together side in the first two rounds because most of their regular squad was in quarantine following a disastrous trip to South Africa, quickly hit back.
Winger Owen Lane powered through a tackle to score after 11 minutes. Eight minutes later No. 8 James Ratti forced his way over to give Cardiff the lead.
Quins dominated the scrums and were rewarded when Cardiff Dmitri Arhip prop was shown a yellow card.
Smith took advantage of the space with a cross kick that yielded a try for fullback Tyrone Green.
After 48 minutes, Smith's deft flick found Alex Dombrandt who sent Luke Northmore over.
After Dombrandt was shown a yellow card in the 53rd minute, Cardiff scored two tries in three minutes: by prop Dillon Lewis, who had replaced Arhip, and Lane to take a 14-point lead.
Danny Care gave Quins hope by squirming over for a try with 20 minutes to play. Smith converted.
Harlequins forced another yellow card as hooker Kirby Myhill paid the price for a series of Cardiff offences close to their own line.
Quins took a scrum. When the ball came out, Smith hitch-kicked, sidestepped and plunged over before kicking the conversion to level the scores.
As time expired, Cardiff, trying to run the ball out from their own half, gave away a penalty for holding onto the ball.
Smith converted to give his team victory.
"To be rewarded with a penalty in front of the sticks by the forwards was a massive relief," Smith said.
Harlequins coach Tabai Matson had mixed feelings.
"Really positive because we got a win away from home," he told BT Sport. "Also really disappointed with the performance."
In contrast, the game in Castres was dominated by the boot.
Fullback Thomas Larregain gave the home team the edge when he collected a clever kick by Ben Botica to touch down after 26 minute.
Botica converted and kicked two penalties while young fly half Jack Crowley kept the visitors in touch with three penalties.
In the closing minutes, Munster opted not to kick for goal and were rewarded when the hulking No.8 Coombes crashed over.