#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long fp(long long x, long long y) {
  if (!y) return 1;
  long long res = fp(x, y / 2);
  res *= res;
  if (y & 1) return res * x;
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    deque<long long> q;
    while (n) q.push_front(n % 3), n /= 3;
    long long x = 0;
    for (int i = q.size() - 1; ~i; i--) {
      if (q[i] == 2) {
        long long j = i, cnt = 1;
        while (cnt && ~j) {
          q[j]++;
          q[j] %= 3;
          cnt = !q[j];
          if (q[j] == 2) cnt = 1, q[j]++, q[j] %= 3;
          j--;
        }
        if (j == -1) x = cnt;
        long long k = i;
        while (k < q.size()) q[k++] = 0;
        i = j + 1;
      }
    }
    if (x) {
      cout << fp(3, q.size()) << '\n';
      continue;
    }
    reverse(q.begin(), q.end());
    long long ans = 0;
    for (int i = 0; i < q.size(); i++) ans += q[i] * fp(3, i);
    cout << ans << '\n';
  }
  return 0;
}
