#include <bits/stdc++.h>
using namespace std;
void in(long long *a, long long n) {
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
}
void out(long long *a, long long n) {
  for (long long i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
}
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  long long t;
  cin >> t;
  long long a[40];
  a[0] = 1;
  for (long long i = 1; i < 40; i++) {
    a[i] = a[i - 1] * 3;
  }
  for (long long nj = 0; nj < t; nj++) {
    long long n;
    cin >> n;
    unsigned long long ans = 0;
    long long i, k;
    vector<long long> v;
    for (i = 39; i >= 0; i--) {
      k = n / a[i];
      v.push_back(k);
      n = n % a[i];
    }
    long long car = 0;
    for (long long j = v.size() - 1; j >= 0; j--) {
      if (v[j] == 0) {
        if (car == 1) {
          ans += a[40 - j - 1];
          car = 0;
        }
      } else {
        if (car == 0) {
          if (v[j] == 1) {
            ans += a[40 - j - 1];
          } else {
            if (v[j] == 2) {
              car = 1;
              ans = 0;
            }
          }
        }
      }
    }
    cout << ans << "\n";
  }
}
