One test case giving WA (MOON)

Here is my solution to the problem MOON:
I tried framing a linear diophantine equation to solve the problem. But one test case is failing, any help is much appreciated.
Proble Link:
Problem Link
Solution:
https://www.codechef.com/viewsolution/31956702