This page is still under construction.

Parts of this page are still being built. What you see may change.

The Site of Sunrin

Time limit1sMemory limit512 MB

Summary
Find the N-th smallest natural number whose base-3 digits are all 0 or 1, for up to 1000 queries with N up to about 1.2e11.
Level

Medium4 of 10

Topics
Math, Combinatorics, Bit manipulation, Implementation
Solved
No attempts yet

Problem

Standing tall on high Namsan

(omitted)

Build it upon bedrock

the site of Sunrin

In 1899, you receive a decree from Emperor Gojong of the Korean Empire and must choose the site on which to build the Government School of Industry and Commerce, Korea's first vocational education institution.

The Korean Empire has several sites suitable for building a school, and each site has a distinct natural number as its number. In particular, the number of the site of Sunrin is a natural number obtained by adding at most one natural number of the form 3k3^k for each k≥0k \ge 0. That is, the numbers of the site of Sunrin include 1(=30),3(=31),9(=32),27(=33),81(=34),90(=32+34),91(=30+32+34)1(=3^0), 3(=3^1), 9(=3^2), 27(=3^3), 81(=3^4), 90(=3^2+3^4), 91(=3^0+3^2+3^4), and so on.

You have been ordered to find the NN-th smallest site of Sunrin. Write a program that finds the NN-th site of Sunrin.

Input

The first line gives TT, the number of sites of Sunrin you must find.

Each of the next TT lines gives NN, the information about one site of Sunrin you must find.

Output

Print the numbers of the sites of Sunrin you must find, one per line, in order.

Constraints

1≤T≤1 0001 \leq T \leq 1\,000

1≤N≤123 456 789 1231 \leq N \leq 123\,456\,789\,123

Hint

The numbers in the input and the answers you must print are very large, so you need a 64-bit type (long long in C/C++, long in Java, printed with %lld).

Examples1

  1. Example 1

    Input
    3
    1
    2
    123456789123
    
    Expected output
    1
    3
    217523656249693825