#include <bits/stdc++.h>
using namespace std;
using ulli = unsigned long long int;
ulli p3(ulli x) {
  ulli ret = 0, ar = 1;
  while (x > 0) {
    ret += ar * (x & 1);
    ar *= 3;
    x /= 2;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  long long int q;
  cin >> q;
  while (q--) {
    ulli n;
    cin >> n;
    unsigned long long int n1 = 1e12;
    long long int l = 1, r = n1;
    while (l + 1 < r) {
      long long int mid = (l + r) / 2;
      long long int ad = p3(mid);
      if (ad < n) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    cout << ((p3(l) < n) ? p3(l + 1) : p3(l)) << '\n';
  }
}
