Alice and Bob always love to play games, so does this time.
It is their favorite stone-taken game.
However, this time they does not compete but co-operate to finish this task.
Suppose there is a stack of n stones.
Each turn,
Alice can only take away stones in number pow of 2, say 1, 2, 4, 8, 16, ...
Bob can only take away stones in number pow of 3, say 1, 3, 9, 27, 81, ...
They takes stones alternately, and lady first.
Notice in each turn, Alice/Bob have to take away at least one stone, unless the stack is empty.
Now, the question is, what is the least number of operation for taking away all the stones.
Multiple test cases. First line, there is an integer T ( 1 ≤ T ≤ 20 ), indicating the number of test cases.
For each test case, there is a number n ( 1 ≤ n ≤ 10000 ), occupying a line, indicating the total number of the stones.
For each test case, output a line. It is an integer number k, indicating the least number of operation in need to finish the task.
5 1 2 3 4 5
1 1 2 1 2View Code(DP) View Code(BFS)