Problem 2 with several test cases.