S&P 500 closes with losses, erases all gains in 5 weeks