n = int(input()) d = {} temp = 1 for y in range(10**3): temp *= 2 d[temp-y] = y if temp > 10**9: break #print(d) for i in range(n): p = int(input()) if p in d: print(p+d[p]) continue for k, v in d.items(): if k%p == v: print(v) break else: print(-1)