https://www.codechef.com/viewsolution/66804443
Submission above gets runtime error while being correct.
The issue is that I have to sanitize the input. Turns out there are characters that are not digits in a test case string line.
https://www.codechef.com/viewsolution/66804572
Submission above passes once I take only digits from a line.