Spp recursive sequence

my solutiion is giving correct output for given test case and i have checked it so many times such that now i am unable to debug it.
please help with it as it is giving wa in some test cases

my code https://ide.codingblocks.com/s/245859

Dry run your code for the input:
1
1 1
1 1 1000000000000000000 1234567
Desired output should be 532900
output from your code is 532899

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance,you still have some questions or not find the answers satisfactory, you may reopen
the doubt.