Hungary and Portugal reached the Euro 2016 knockout stages after a rip-roaring 3-3 draw in which Cristiano Ronaldo netted twice and became the first player to score at four European Championship finals on Wednesday.
The Portuguese came from behind three times as Ronaldo, who also set a tournament record of 17 appearances, finally rediscovered his touch to help Portugal make the last 16, where they will meet Croatia in Lens on Saturday.
Group F winners Hungary finished above Iceland, who also qualified after a last-gasp 2-1 win over Austria in Paris, and third-placed Portugal.
Hungary will play the Group E runners-up, which will be decided later on Wednesday between Belgium, Sweden and Ireland, while Iceland will face England.
Ronaldo equalised twice in the second half after Zoltan Gera's early strike was cancelled out by Nani just before the break. Hungary captain Balazs Dzsudzsak had twice put his side ahead before Ronaldo burst into life with a double strike.
"We're happy as we're still in the tournament. It was a tough game but we created chances and se scored goals," said Portugal coach Fernando Santos.
Portugal had promised the goals would flow and they did.
Like in their previous two matches against Iceland and Austria, Portugal enjoyed sterile possession in the early stages and rarely troubled their rivals with a flurry of crosses easily dealt with by the Hungarian defence.
With Ronaldo totally subdued, Hungary looked more likely to score through sporadic but dangerous raids.
In the 19th minute, the 37-year old Gera took a half-cleared corner into his stride and unleashed a fizzing low-half volley past Rui Patricio from 25 metres.
Rocked by the setback, Portugal nearly fell two goals behind several minutes later after Patricio was forced to parry a stinging shot by the hardworking Akos Elek.
However, Ronaldo, struggling for form at Euro 2016, released Nani with a defence-splitting pass and his strike partner beat Gabor Kiraly with a neat finish from inside the penalty area three minutes before the break.
The Hungarians were back in front in the 47th minute as Dzsudzsak beat Patricio with a free kick.
After throwing a reporter's microphone into a lake earlier in the day during a morning walkabout, Ronaldo produced his best moment of the tournament in the 50th, beating Kiraly with a clever back-heel after good work by Nani.
Hungary took the lead for the third time.
Having had another free kick blocked by the wall, Dzsudsak took the rebound in his stride on the edge of the area and fired a speculative shot which sailed past Patricio.
Hungary's joy was short-lived, however, as Ronaldo levelled with a thumping header after a superb cross by substitute Ricardo Quaresma, drawing an eruption of joy from the Portuguese fans behind Kiraly's goal.