#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
  for (int(i) = 0; (i) < vec.size(); ++(i)) os << vec[i] << ' ';
  return os;
}
ull pows(ull n, ull s) {
  if (s == 0) return 1;
  ull ans = 0;
  if (s & 1) {
    ans = pows(n, s - 1);
    ans *= n;
  } else {
    ans = pows(n, s / 2);
    ans *= ans;
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  ull q;
  cin >> q;
  while (q--) {
    ull n;
    cin >> n;
    ull tmp = n;
    bool was_2 = 0;
    bool was_after = 0;
    ull after = -1;
    ull curp = 1;
    while (tmp >= 0) {
      if (tmp % 3 == 2) {
        was_2 = 1;
        was_after = 0;
      } else if (tmp % 3 == 0) {
        if (was_2 && !was_after) {
          was_after = 1;
          after = curp;
        }
      }
      curp *= 3;
      if (tmp == 0) break;
      tmp /= 3;
    }
    if (was_2) {
      if (after > n)
        cout << after;
      else {
        ull curp = 1;
        ull ans = n + after;
        while (curp != after) {
          ans -= n % 3 * curp;
          curp *= 3;
          n /= 3;
        }
        cout << ans;
      }
    } else
      cout << n;
    cout << '\n';
  }
  return 0;
}
