#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int rem[50] = {0};
    long long int d;
    long long int k = 0;
    while (n > 0) {
      d = n % 3;
      rem[k] = d;
      k++;
      n /= 3;
    }
    for (long long int i = 0; i < k; i++) {
      if (rem[i] >= 2) {
        rem[i + 1] += 1;
        for (long long int j = 0; j <= i; j++) {
          rem[j] = 0;
        }
      }
    }
    long long int it = 1;
    long long int ans = 0;
    for (long long int i = 0; i <= k; i++) {
      ans += rem[i] * it;
      it *= 3;
    }
    cout << ans << endl;
  }
}
