#include <bits/stdc++.h>
using namespace std;
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
bool prime[200005];
void SieveOfEratosthenes(long long int n) {
  memset(prime, true, sizeof(prime));
  for (long long int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
bool isPrime(long long int n) {
  if (n < 2) return false;
  for (long long int i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
std::vector<long long int> generatePrimeFactors(long long int n) {
  std::vector<long long int> prime;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      prime.push_back(i);
      while (n % i == 0) n = n / i;
    }
  }
  if (n != 1) prime.push_back(n);
  return prime;
}
std::vector<long long int> generateFactors(long long int n) {
  std::vector<long long int> fact;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      fact.push_back(i);
      if (n / i != i) fact.push_back(n / i);
    }
  }
  fact.push_back(1);
  fact.push_back(n);
  sort(fact.begin(), fact.end());
  return fact;
}
long long int gcd(long long int a, long long int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long int modpow(long long int x, long long int n, long long int m) {
  if (n == 0) return 1 % m;
  long long int u = modpow(x, n / 2, m) % m;
  u = ((u % m) * (u % m)) % m;
  if (n % 2 == 1) u = ((u % m) * (x % m)) % m;
  return u % m;
}
long long int eulerstotientfunction(long long int n) {
  long long int ans = n;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n = n / i;
      ans -= ans / i;
    }
  }
  if (n > 1) ans -= ans / n;
  return ans;
}
long long int modinverse(long long int x, long long int m) {
  return modpow(x, m - 2, m);
}
long long int add(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  return (((a % m) + (b % m)) % m);
}
long long int substract(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  return (((a % m) - (b % m) + m) % m);
}
long long int multiply(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  return (((a % m) * (b % m)) % m);
}
long long int divide(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  long long int temp = modinverse(b, m);
  return multiply(a, temp, m);
}
long long int singlemod(long long int a, long long int m) {
  a = ((a % m) + m) % m;
  return (a % m);
}
long long int ncr(long long int n, long long int k, long long int m) {
  if (k > n) return 0;
  long long int res = 1;
  k = min(k, n - k);
  for (long long int i = 1; i <= k; i++) {
    res = multiply(res, n - i + 1, m);
    res = divide(res, i, m);
  }
  return singlemod(res, m);
}
long long int kadaneAlgo(const std::vector<long long int> &arr) {
  long long int size = arr.size();
  long long int currmax = arr[0], maxsofar = arr[0];
  for (long long int i = 1; i < size; i++) {
    currmax = max(arr[i], currmax + arr[i]);
    maxsofar = max(maxsofar, currmax);
  }
  return maxsofar;
}
long long int getDigitSum(long long int n) {
  long long int co = 0;
  while (n > 0) {
    co += n % 10;
    n = n / 10;
  }
  return co;
}
template <class T>
void printVector(const std::vector<T> arr) {
  for (auto i : arr) cout << i << " ";
  cout << '\n';
}
template <class T>
void printArray(T arr[], long long int n) {
  for (long long int i = 1; i <= n; i++) cout << arr[i] << " ";
  cout << '\n';
}
int main() {
  fastio();
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int temp = n;
    string str;
    while (n) {
      str += ((n % 3) + '0');
      n /= 3;
    }
    n = str.length();
    long long int posleftmost2 = -1;
    for (long long int i = n - 1; i >= 0; i--) {
      if (str[i] == '2') {
        posleftmost2 = i;
        break;
      }
    }
    if (posleftmost2 == -1) {
      cout << temp << '\n';
      continue;
    }
    posleftmost2++;
    while (posleftmost2 < n) {
      if (str[posleftmost2] == '0') {
        str[posleftmost2] = '1';
        break;
      }
      posleftmost2++;
    }
    for (long long int i = 0; i < posleftmost2; i++) str[i] = '0';
    if (posleftmost2 == n) {
      str += '1';
      n++;
    }
    long long int ans = 0;
    for (long long int i = 0; i < n; i++) {
      ans += (str[i] - '0') * (pow(3, i));
    }
    cout << ans << '\n';
    fprintf(stderr, "Time: %d ms\n", (int)(clock() * 1000.0 / CLOCKS_PER_SEC));
  }
}

### Prompt

Create a solution in CPP for the following problem:

The only difference between easy and hard versions is the maximum value of n.

You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.

The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).

For example:

  * 30 is a good number: 30 = 3^3 + 3^1, 
  * 1 is a good number: 1 = 3^0, 
  * 12 is a good number: 12 = 3^2 + 3^1, 
  * but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0), 
  * 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid), 
  * 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid). 



Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.

For the given positive integer n find such smallest m (n ≤ m) that m is a good number.

You have to answer q independent queries.

Input

The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. Then q queries follow.

The only line of the query contains one integer n (1 ≤ n ≤ 10^4).

Output

For each query, print such smallest integer m (where n ≤ m) that m is a good number.

Example

Input


7
1
2
6
13
14
3620
10000


Output


1
3
9
13
27
6561
19683

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
bool prime[200005];
void SieveOfEratosthenes(long long int n) {
  memset(prime, true, sizeof(prime));
  for (long long int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
bool isPrime(long long int n) {
  if (n < 2) return false;
  for (long long int i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
std::vector<long long int> generatePrimeFactors(long long int n) {
  std::vector<long long int> prime;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      prime.push_back(i);
      while (n % i == 0) n = n / i;
    }
  }
  if (n != 1) prime.push_back(n);
  return prime;
}
std::vector<long long int> generateFactors(long long int n) {
  std::vector<long long int> fact;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      fact.push_back(i);
      if (n / i != i) fact.push_back(n / i);
    }
  }
  fact.push_back(1);
  fact.push_back(n);
  sort(fact.begin(), fact.end());
  return fact;
}
long long int gcd(long long int a, long long int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long int modpow(long long int x, long long int n, long long int m) {
  if (n == 0) return 1 % m;
  long long int u = modpow(x, n / 2, m) % m;
  u = ((u % m) * (u % m)) % m;
  if (n % 2 == 1) u = ((u % m) * (x % m)) % m;
  return u % m;
}
long long int eulerstotientfunction(long long int n) {
  long long int ans = n;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n = n / i;
      ans -= ans / i;
    }
  }
  if (n > 1) ans -= ans / n;
  return ans;
}
long long int modinverse(long long int x, long long int m) {
  return modpow(x, m - 2, m);
}
long long int add(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  return (((a % m) + (b % m)) % m);
}
long long int substract(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  return (((a % m) - (b % m) + m) % m);
}
long long int multiply(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  return (((a % m) * (b % m)) % m);
}
long long int divide(long long int a, long long int b, long long int m) {
  a = ((a % m) + m) % m;
  b = ((b % m) + m) % m;
  long long int temp = modinverse(b, m);
  return multiply(a, temp, m);
}
long long int singlemod(long long int a, long long int m) {
  a = ((a % m) + m) % m;
  return (a % m);
}
long long int ncr(long long int n, long long int k, long long int m) {
  if (k > n) return 0;
  long long int res = 1;
  k = min(k, n - k);
  for (long long int i = 1; i <= k; i++) {
    res = multiply(res, n - i + 1, m);
    res = divide(res, i, m);
  }
  return singlemod(res, m);
}
long long int kadaneAlgo(const std::vector<long long int> &arr) {
  long long int size = arr.size();
  long long int currmax = arr[0], maxsofar = arr[0];
  for (long long int i = 1; i < size; i++) {
    currmax = max(arr[i], currmax + arr[i]);
    maxsofar = max(maxsofar, currmax);
  }
  return maxsofar;
}
long long int getDigitSum(long long int n) {
  long long int co = 0;
  while (n > 0) {
    co += n % 10;
    n = n / 10;
  }
  return co;
}
template <class T>
void printVector(const std::vector<T> arr) {
  for (auto i : arr) cout << i << " ";
  cout << '\n';
}
template <class T>
void printArray(T arr[], long long int n) {
  for (long long int i = 1; i <= n; i++) cout << arr[i] << " ";
  cout << '\n';
}
int main() {
  fastio();
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int temp = n;
    string str;
    while (n) {
      str += ((n % 3) + '0');
      n /= 3;
    }
    n = str.length();
    long long int posleftmost2 = -1;
    for (long long int i = n - 1; i >= 0; i--) {
      if (str[i] == '2') {
        posleftmost2 = i;
        break;
      }
    }
    if (posleftmost2 == -1) {
      cout << temp << '\n';
      continue;
    }
    posleftmost2++;
    while (posleftmost2 < n) {
      if (str[posleftmost2] == '0') {
        str[posleftmost2] = '1';
        break;
      }
      posleftmost2++;
    }
    for (long long int i = 0; i < posleftmost2; i++) str[i] = '0';
    if (posleftmost2 == n) {
      str += '1';
      n++;
    }
    long long int ans = 0;
    for (long long int i = 0; i < n; i++) {
      ans += (str[i] - '0') * (pow(3, i));
    }
    cout << ans << '\n';
    fprintf(stderr, "Time: %d ms\n", (int)(clock() * 1000.0 / CLOCKS_PER_SEC));
  }
}

```