#include <bits/stdc++.h>
using namespace std;
const int MN = 3e5 + 5;
const int P = 998244353;
vector<long long> v;
long long s[50];
inline void solve() {
  long long n;
  scanf("%lld", &n);
  long long ans = s[39];
  for (int i = 39; i >= 1; --i)
    if (ans - v[i] >= n) ans -= v[i];
  printf("%lld\n", ans);
}
int main(int argc, char** argv) {
  long long bs = 1;
  v.push_back(0);
  do {
    v.push_back(bs);
    bs *= 3;
  } while (bs < (1ll << 61));
  for (int i = 1; i <= 39; ++i) s[i] = s[i - 1] + v[i];
  int _;
  scanf("%d", &_);
  for (int i = 0; i < _; ++i) solve();
  return 0;
}
