#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18 + 5;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 314;
const long double pi = 3.1415926535897932384626433832795;
void solve() {
  ll n;
  cin >> n;
  ll cop = n;
  vector<ll> con;
  while (n > 0) {
    con.push_back(n % 3);
    n /= 3;
  }
  ll sum = 0, mnd = INF;
  map<ll, ll> u;
  for (ll i = (int)con.size() - 1; i >= 0; i--) {
    if (!con[i]) {
      if (sum + pow(3, i) >= cop) mnd = min(mnd, sum + (ll)pow(3, i));
      continue;
    }
    sum += pow(3, i);
  }
  ll pre = pow(3, (int)con.size());
  if (sum == cop)
    cout << sum;
  else if (mnd == INF)
    cout << pre;
  else
    cout << mnd;
  cout << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  ll t;
  cin >> t;
  while (t--) solve();
}
