Getting WA in my submission and im getting correct output in starters 9 in BUS

here is my submission link
https://www.codechef.com/viewsolution/49965726

Your code is looking for 5 queries in this testcase
1
5 1 4
+1
+2
+3
+4

It should print “INCONSISTENT” but your code only after getting 5th input it prints the solution

thanks i got it w=g must be replaced with w=g+1