SNP wins third term but loses majority