mathematicians-fight-4

Once a Mathematician was trapped by military people during a world war with his forty colleagues. The colleagues were without any weapons, only the mathematician had a knife. And with no weapons they were not able to fight with the military, so they decided to kill themselves rather than being killed by military.
They all decided that they will stand in a circle and the every third person will 
kill himself but the mathematician didn't like this idea and had no intention 
of killing himself. He tried to calculate where should he stand so that he is the last one left.
Note:- Instead of every 3rd position it will be every 2nd position and of course the number of colleagues will be much more than 40.

Input:-
The first line of input is an integer N  that specifies the number of test cases. After that every line contains an integer X  which is the number of colleagues.

Output:-
For each test case generate a line containing the position of the colleagues who survives. Assume that the colleagues have serial numbers from 1 to n and that the counting starts with person 1, i.e., the first person leaving is the one with number 2.

Constraints:-
1 ≤ N ≤ 1000
5 ≤ X ≤ 100000000

SAMPLE INPUT
4
5
11
45
23987443

SAMPLE OUTPUT
3
7
27
14420455

Source: hackerearth | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
import math
def main():
	t=int(input())
	while t!=0:
		t=t-1
		n=int(input())
		l=1+math.floor(math.log(n,2))
		ans=1+(2*n)-(2**l)
		print(int(ans))
if __name__=='__main__':
	main()


t=eval(input())
while t>0:
	t=t-1
	a=eval(input())
	if a&(a-1)==0:
		print("1\n")
	else:
		n=a
		p=2
		m=0
		while p<n:
			m=m+1
			p=2*p
		p=p/2
		print(2*(n-p)+1)


from math import log
for _ in range(int(input())):
	X = int(input())
	print(2 * (X - 2 ** int(log(X, 2))) + 1)



import math
cases = int(input())

for case in range(cases):
	n = int(input())
	k = int(math.log(n, 2))
	answer = 2*n - 2**(k+1) +1
	print(answer)