MySolver working for all test cases, unified into single while loop