#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long int n, pow_p, cum = 0, i, pow_n = 1;
    cin >> n;
    pow_p = log(n) / log(3) + 1;
    vector<long long int> vec;
    vec.push_back(1);
    for (i = 1; i <= pow_p; ++i) {
      pow_n *= 3;
      vec.push_back(pow_n);
    }
    for (i = 0; i < vec.size(); ++i) {
      cum += vec[i];
      if (cum >= n) break;
    }
    for (i = (vec.size()) - 1; i >= 0; --i) {
      long long int tem = (cum - vec[i]);
      if (tem >= n) {
        cum -= vec[i];
      }
    }
    cout << cum << '\n';
    vec.clear();
  }
  return 0;
}
