#include <bits/stdc++.h>
using namespace std;
const unsigned long long INF = 1e18 + 10;
unsigned long long Q, N;
vector<unsigned long long> v;
bool has[50];
void first_clear() {
  for (int i = 0; i < 50; i++) has[i] = 0;
}
void psolve() {
  unsigned long long x = 1;
  while (x < INF) {
    v.push_back(x);
    x = x + x + x;
  }
}
unsigned long long get_ans(int lst) {
  for (int i = lst + 1; i < v.size(); i++) {
    if (has[i])
      has[i] = 0;
    else {
      has[i] = 1;
      break;
    }
  }
  unsigned long long ans = 0ll;
  for (int i = 0; i < v.size(); i++) ans += v[i] * has[i];
  return ans;
}
void solve() {
  cin >> N;
  if (N > 675425858836496044) {
    cout << "1350851717672992089" << endl;
    return;
  }
  unsigned long long val = N, ans;
  for (int i = (int)v.size() - 1; i >= 0; i--) {
    if (v[i] * 2 <= val) {
      ans = get_ans(i);
      break;
    }
    if (v[i] <= val) {
      val -= v[i];
      has[i] = 1;
    }
  }
  if (val == 0) ans = N;
  cout << ans << endl;
}
signed main() {
  psolve();
  cin >> Q;
  while (Q--) {
    first_clear();
    solve();
  }
  return 0;
}
