t=int(raw_input())
for i in range(t):
	n=long(raw_input())
	if n&(n-1)==0:
		print long((n*(n+2)*(2*n+1))/8)
	else:
		print long((n*(n+2)*(2*n+1)-1)/8)

I wrote this code and I got a WA. But same code in c++ got accepted. confused How should I increase the range of solution?

i later edited it to

t=int(raw_input())
for i in range(t):
	n=long(raw_input())
	if n&(n-1)==0:
		k= long((n*(n+2)*(2*n+1))/8)
	else:
		k= long((n*(n+2)*(2*n+1)-1)/8)
	if k<0:
		k=k&0xffffffffffffffff
	print k

it passed the 5th test but gave wa on 8.