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
#include <bits/stdc++.h>
using namespace std;
inline bool isLow(char ch) { return (ch >= 'a' && ch <= 'z'); }
inline bool isUpp(char ch) { return (ch >= 'A' && ch <= 'Z'); }
inline bool isDig(char ch) { return (ch >= '0' && ch <= '9'); }
inline bool are_equal(double a, double b) {
  return fabs(a - b) < numeric_limits<double>::epsilon();
}
inline bool less_than(double a, double b) {
  return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) *
                    numeric_limits<double>::epsilon());
}
inline bool greater_than(double a, double b) {
  return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) *
                    numeric_limits<double>::epsilon());
}
template <typename T, typename... args>
inline string arrStr(T arr, int n) {
  stringstream s;
  s << "[ ";
  for (int i = 0; i < n - 1; i++) s << arr[i] << ", ";
  s << arr[n - 1] << " ]";
  return s.str();
}
inline void __evars_begin(int line) { cout << "#" << line << ": "; }
template <typename T>
inline void __evars_out_var(vector<T> val) {
  cout << arrStr(val, val.size());
}
template <typename T>
inline void __evars_out_var(T *val) {
  cout << arrStr(val, 10);
}
template <typename T>
inline void __evars_out_var(T val) {
  cout << val;
}
inline void __evars(vector<string>::iterator it) { cout << endl; }
template <typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
  cout << it->substr((*it)[0] == ' ', it->length()) << " = ";
  __evars_out_var(a);
  cout << "; ";
  __evars(++it, args...);
}
inline vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.emplace_back(x);
  return move(v);
}
void in(int &n, int a[]) {
  cin >> n;
  for (__typeof(n - 1) i = (0); i <= (n - 1); i++) cin >> a[i];
}
void in(int &n, vector<int> &a) {
  cin >> n;
  a.resize(n);
  for (__typeof(n - 1) i = (0); i <= (n - 1); i++) cin >> a[i];
}
void out(int n, int a[]) {
  for (__typeof(n - 1) i = (0); i <= (n - 1); i++)
    cout << a[i] << " \n"[i + 1 == n];
}
void out(vector<int> &a) {
  for (__typeof(a.size() - 1) i = (0); i <= (a.size() - 1); i++)
    cout << a[i] << " \n"[i + 1 == a.size()];
}
template <typename T = long long>
T multiply(T a, T b, T mod) {
  T res = 0;
  a %= mod;
  b %= mod;
  while (b) {
    if (b & 0x1) res = (res + a) % mod;
    b >>= 1;
    if (a < mod - a)
      a <<= 1;
    else
      a -= (mod - a);
  }
  return res;
}
template <typename T = long long>
T power(T val, T p, T mod) {
  val %= mod;
  T res = 1;
  while (p > 0) {
    if (p & 0x1) res = (res * val) % mod;
    val = (val * val) % mod;
    p >>= 1;
  }
  return res;
}
template <typename T = long long>
T power(T val, T p) {
  T res = 1;
  while (p > 0) {
    if (p & 0x1) res = (res * val);
    val = (val * val);
    p >>= 1;
  }
  return res;
}
template <typename T = long long>
T powerMul(T val, T p, T mod) {
  val %= mod;
  T res = 1;
  while (p > 0) {
    if (p & 0x1) res = multiply(res, val, mod);
    val = multiply(val, val, mod);
    p >>= 1;
  }
  return res;
}
template <typename T>
T gcd(T a, T b) {
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <typename T>
string toString(T x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
template <typename T>
T stringToT(string s) {
  T p;
  istringstream ss(s);
  ss >> p;
  return p;
}
template <typename T>
T SOD(T n) {
  __typeof(n) sum = 0, i = 1;
  for (; i * i <= n; i++)
    sum += (n % i) ? 0 : ((((i) * (i)) == n) ? i : i + n / i);
  return sum;
}
const int fx[] = {+1, -1, +0, +0};
const int fy[] = {+0, +0, +1, -1};
const int fx8[] = {+0, +0, +1, -1, -1, +1, -1, +1};
const int fy8[] = {-1, +1, +0, +0, +1, +1, -1, -1};
const int kx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int ky[] = {-1, 1, -2, 2, -2, 2, -1, 1};
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
const int mod = 1e9 + 7;
const int mx = 2e5 + 5;
int main() {
  unsigned long long v = 1LL;
  vector<unsigned long long> vec;
  for (unsigned long long i = 0; i <= 35; i++) {
    vec.push_back(v);
    v *= 3LL;
  }
  vector<unsigned long long> va, vb;
  for (int i = 0; i < 36; i++)
    if (i & 1)
      va.push_back(vec[i]);
    else
      vb.push_back(vec[i]);
  vector<unsigned long long> vsa, vsb;
  vsa.push_back(0);
  vsb.push_back(0);
  int n = va.size();
  for (int i = 1; i < (1 << n); i++) {
    unsigned long long sum = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) sum += va[j];
    vsa.push_back(sum);
  }
  n = vb.size();
  for (int i = 1; i < (1 << n); i++) {
    unsigned long long sum = 0;
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) sum += vb[j];
    vsb.push_back(sum);
  }
  sort(vsb.begin(), vsb.end());
  vsa.push_back(1350851717672992089LL);
  int cases, tc;
  cin >> tc;
  for (cases = 1; cases <= tc; cases++) {
    unsigned long long n;
    cin >> n;
    unsigned long long m;
    int f = 0;
    for (int i = 0; i < vsa.size(); i++) {
      unsigned long long u = vsa[i];
      if (u >= n) {
        if (f == 0) {
          m = u;
          f = 1;
        } else
          m = min(m, u);
        break;
      }
      unsigned long long val = *lower_bound(vsb.begin(), vsb.end(), n - u);
      if (u + val >= n && f == 0) {
        m = u + val;
        f = 1;
      } else if (u + val >= n)
        m = min(m, u + val);
    }
    cout << m << '\n';
  }
  return 0;
}
