#include <bits/stdc++.h>
using namespace std;
void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int32_t main() {
  optimizeIO();
  long long tt = 1;
  cin >> tt;
  while (tt--) {
    long long n;
    cin >> n;
    vector<bool> v(65, 0);
    while (n) {
      long long temp = 1;
      long long ind = 0;
      while (temp < n) {
        temp *= 3;
        ind++;
      }
      if (temp != n) {
        temp /= 3;
        ind--;
      }
      if (!v[ind])
        v[ind] = true;
      else {
        for (long long i = ind; i < 65; i++) {
          if (v[i])
            v[i] = !v[i];
          else {
            v[i] = true;
            break;
          }
        }
        break;
      }
      n -= temp;
    }
    long long res = 0;
    for (long long i = (long long)(0); i < (long long)(65); i++) {
      if (v[i]) {
        long long temp = 1;
        for (long long j = (long long)(0); j < (long long)(i); j++) {
          temp *= 3;
        }
        res += temp;
      }
    }
    cout << res << endl;
  }
  return 0;
}
