#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const long long INF1 = 998244353;
const unsigned long long LLINF = (long long)4e18;
const int INTmx = (int)1e9;
const int N = 2e5 + 300;
int was[N];
long long dd[N];
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (int tt = 1; tt <= q; tt++) {
    long long n;
    cin >> n;
    for (int i = 0; i <= 50; i++) was[i] = 0;
    int kol = 0;
    long long t = 1ll;
    while (n) {
      long long k = n % 3ll;
      was[kol] = k;
      dd[kol] = t;
      n /= 3ll;
      kol++;
      t *= 3ll;
    }
    long long ans = LLINF, ans1 = 0;
    for (int i = kol; i >= 0; i--) {
      if (dd[i] == 0 && i > 0) dd[i] = dd[i - 1] * 3ll;
      if (was[i] == 2) {
        was[i + 1]++;
        ans = 0;
        ans1 = LLINF;
        for (int j = i + 1; j <= kol; j++) {
          if (was[j] == 1) {
            ans += dd[j];
          } else if (was[j] == 2) {
            was[j + 1]++;
          }
        }
        break;
      }
      if (was[i] == 1) ans1 += dd[i];
    }
    ans = min(ans, t);
    cout << min(ans, ans1) << endl;
  }
}
