#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
vector<ll> p3;
ll n, m;
void check() {
  cin >> n;
  m = 0ll;
  while (n > 0) {
    ll i;
    for (i = 0; i < 39; i++)
      if (p3[i] < n) break;
    if (n - p3[i] <= p3[i] / 2) {
      n -= p3[i];
      m += p3[i];
    } else {
      n -= p3[i - 1];
      m += p3[i - 1];
    }
  }
  cout << m << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll q;
  cin >> q;
  for (ll i = 39; i >= 0; i--) p3.push_back(pow(3ll, i));
  for (ll i = 0; i < q; i++) check();
  return 0;
}
