#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  for (long long l = 0; l < t; l++) {
    long long n;
    cin >> n;
    long long n1 = n;
    long long k = -1, p = 1;
    while (n1 > 0) {
      n1 = n1 / 3;
      k++;
      p = p * 3;
    }
    long long p1 = p;
    long long a[k + 1];
    bool b[k + 1];
    for (long long i = 0; i <= k + 1; i++) {
      b[i] = true;
    }
    long long x = -1;
    while (x != (k + 1)) {
      x++;
      a[x] = n / p;
      n = n - a[x] * p;
      p = p / 3;
    }
    for (long long i = x; i >= 1; i--) {
      if (a[i] > 1) {
        b[i] = false;
        a[i] = a[i] - 2;
        a[i - 1]++;
      }
    }
    long long tong = 0;
    for (long long i = 0; i <= x; i++) {
      if (b[i] == true) {
        tong = tong + a[i] * p1;
        p1 = p1 / 3;
      } else
        break;
    }
    cout << tong << '\n';
  }
}
