AC Milan avoided being eliminated in a preliminary round in Europe for the first time on Thursday by equalising with a penalty in the last seconds of extra time and then winning a 24-kick shootout 9-8.
The seven-times European champions trailed going into added time in extra time but Toni Borevkovic handled in the penalty area, earning his second yellow card and Hakan Calhanoglu converted from the spot to give Milan a 2-2 draw and set up an agonising shootout.
"You can't win without suffering," said Milan manager Stefano Pioli.
Elsewhere, former European champions Red Star Belgrade, Celtic and PSV Eindhoven all advanced with away victories.
Tottenham romped to a 7-2 victory over Maccabi Haifa, Granada, competing in Europe for the first time, won 3-1 in Malmo but Basel let in three goals in the last 18 minutes and lost 3-1 at home to CSKA Sofia while Bundesliga club Wolfsburg also blew a lead and went down 2-1 to AEK in Athens.
In Northern Portugal, Belgian attacker Alexis Saelemaekers put Milan ahead with a low shot from the edge of the penalty area after 51 minutes.
Francisco Geraldes levelled after 71 minutes with a first-time shot.
In the first minute of extra time, Gelson put the home team ahead.
Milan had 24 strikes on goal but only levelled when Berokovic jumped to head a harmless lob, missed the ball and was struck on the arm. Calhanoglu drilled the spot kick in off the base of the post.
Milan went first in the shootout. Both teams converted their first six attempts. In the next four rounds, Rio Ave spurned three chances to win; following a Milan miss with one of their own.
In that sequence, Nelson Monte hit both posts before his shot rolled away from goal, the two goalkeepers took consecutive kicks and both blasted over and Geraldes also hit a post
The sequence was broken when defender Simon Kjaer converted for the second time in the shootout and then Gianluigi Donnarumma hurled himself high to his right to push away Nuno Santos' penalty.
The Milan players celebrated with extraordinary energy.
"It's difficult to control emotions," said Pioli. "We went from winning to losing in a moment and vice versa."
Milan were without a host of experienced players including Zlatan Ibrahimovic.
"We were strong, especially mentally, with a very young team," said Pioli.
In London, Harry Kane scored a hat-trick for the hosts. Giovani Lo Celso tripled his total of goals for the club with two in three minutes, Lucas Moura and Dele Alli, with his first goal since March, also scored.
Celtic squeezed through 1-0 after Odsonne Edouard prodded home a rebound midway through the second half away to FK in Sarajevo.
In Basel, the home team took the lead after 54 minutes when Arthur Cabral converted a penalty but Portuguese midfielder Tiago Rodrigues levelled after 72 minutes and then put CSKA ahead in the 88th minute. Ahmed Ahmedov added a third deep into added time.
In Athens, Swiss striker Adir Mehmedi gave Wolfsburg the lead in first-half stoppage time but Portuguese midfielder Andre Simoes replied after 64 minutes and Iranian attacker Karim Ansarifard hit the winner four minutes into added time.