Cape Town - Scotland have beaten Samoa at St James' Park in Newcastle to finish as runners-up in Pool B - and break Japanese hearts.
Scotland won 36-33 after Samoa led 26-23 at half-time.
Scotland will play whoever wins Saturday's (17:45) Australia v Wales match at Twickenham, in the quarter-finals.
Japan, who needed Samoa to win in order to stand any chance of progressing to the playoffs, now face a 'dead' rubber against the USA at Kingsholm on Sunday.
Japan are already sure of a spot at the 2019 Rugby World Cup as hosts.
Tries: Tommy Seymour, John Hardie, Greig Laidlaw
Conversions: Laidlaw (3)
Penalties: Laidlaw (5)
Tries: Tusi Pisi, Manu Leiataua, Reynold Lee-Lo, Motu Matu'u
Conversions: Pisi, Patrick Fa'apale
Penalties: Pisi (3)
15 Stuart Hogg, 14 Tommy Seymour, 13 Mark Bennett, 12 Matt Scott, 11 Sean Maitland, 10 Finn Russell, 9 Greig Laidlaw (captain), 8 David Denton, 7 John Hardie, 6 Ryan Wilson, 5 Jonny Gray, 4 Richie Gray, 3 WP Nel, 2 Ross Ford, 1 Alasdair Dickinson
Substitutes: 16 Fraser Brown, 17 Gordon Reid, 18 Jon Welsh, 19 Tim Swinson, 20 Josh Strauss, 21 Henry Pyrgos, 22 Peter Horne, 23 Sean Lamont
15 Tim Nanai-Williams, 14 Paul Perez, 13 George Pisi, 12 Rey Lee-Lo, 11 Fa'atoina Autagavaia, 10 Tusi Pisi, 9 Kahn Fotuali'i (captain), 8 Alafoti Faosiliva, 7 Jack Lam, 6 Maurie Faasavalu, 5 Kane Thompson, 4 Teofilo Paulo, 3 Census Johnston, 2 Ma'atulimanu Leiataua, 1 Sakari Taulafo
Substitutes: 16 Motu Matu'u, 17 Viliamu Afatia, 18 Anthony Perenise, 19 Faifili Levave, 20 Vavae Tuilagi, 21 Vavao Afemai, 22 Patrick Faapale, 23 Ken Pisi