#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double PI = acos(-1);
const long long MOD = 1000000007;
const long long FMOD = 998244353;
const long double eps = 1e-9;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long prod = 1, sum = 0;
    while (sum <= n) {
      sum += prod;
      prod *= 3;
    }
    while (prod) {
      if (sum - prod >= n) {
        sum -= prod;
      }
      prod /= 3;
    }
    cout << sum << endl;
  }
}
