atcoder#ARC161B. [ARC161B] Exactly Three Bits

[ARC161B] Exactly Three Bits

Score : 400400 points

Problem Statement

For a positive integer XX, let f(X)f(X) be the number of 11s that appear when XX is represented in binary notation. For example, $6 = 110_{(2)}, \ 11 = 1011_{(2)}, \ 16 = 10000_{(2)}$, so f(6)=2, f(11)=3, f(16)=1f(6) = 2, \ f(11) = 3, \ f(16) = 1.

You are given a positive integer NN. Determine whether there is a positive integer XX less than or equal to NN such that f(X)=3f(X) = 3, and if so, find the maximum such XX.

There are TT test cases to solve.

Constraints

  • 1T1051 \leq T \leq 10^5
  • 1N10181 \leq N \leq 10^{18}

Input

The input is given from Standard Input in the following format, where NiN_i represents the value of NN in the ii-th test case:

TT

N1N_1

N2N_2

\vdots

NTN_T

Output

Print TT lines. The ii-th line should contain the maximum positive integer XX less than or equal to NiN_i such that f(X)=3f(X) = 3. If there is no such positive integer XX, print -1.

4
16
161
4
1000000000000000000
14
161
-1
936748722493063168
  • For the first test case, f(14)=3, f(15)=4, f(16)=1f(14) = 3, \ f(15) = 4, \ f(16) = 1, so the maximum positive integer XX satisfying X16X \leq 16 and f(X)=3f(X) = 3 is 1414.
  • For the second test case, f(161)=3f(161) = 3, so the maximum positive integer XX satisfying X161X \leq 161 and f(X)=3f(X) = 3 is 161161.
  • For the third test case, f(1)=1, f(2)=1, f(3)=2, f(4)=1f(1) = 1, \ f(2) = 1, \ f(3) = 2, \ f(4) = 1, so there is no positive integer XX satisfying X4X \leq 4 and f(X)=3f(X) = 3.
  • As seen in the fourth test case, the input and output values may not fit in a 32-bit integer type.