#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
const ll mod = 1000000007;
const ll INF = 1000000099;
const ll LINF = (ll)(1e18 + 99);
vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
template <typename T, typename U>
void chmin(T &t, const U &u) {
  if (t > u) t = u;
}
template <typename T, typename U>
void chmax(T &t, const U &u) {
  if (t < u) t = u;
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
T mpow(T a, T n) {
  T res = 1;
  for (; n; n >>= 1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}
signed main() {
  int q;
  cin >> q;
  while (q--) {
    ll n, x, cnt = 0, ans, t = -1;
    cin >> n;
    x = n;
    while (x > 0) {
      if (x % 3 == 2) {
        t = cnt;
      }
      cnt++;
      x /= 3;
    }
    ans = n;
    if (t >= 0) {
      ans /= mpow(3ll, t);
      x = ans;
      ans *= mpow(3ll, t);
      ans += mpow(3ll, t);
      x++;
      cnt = t;
      while (x > 0) {
        if (x % 3 == 2) {
          ans += mpow(3ll, cnt);
          x++;
        }
        cnt++;
        x /= 3;
      }
    }
    cout << ans << endl;
  }
}
