#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << '\n';
  err(++it, args...);
}
const long long inf = 1e18L + 5;
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    long long num = n;
    vector<long long> vals;
    long long pos2 = -1;
    while (n > 0) {
      vals.push_back(n % 3);
      if (vals.back() == 2) {
        pos2 = vals.size() - 1;
      }
      n /= 3;
    }
    vals.push_back(0);
    if (pos2 != -1) {
      long long pos0 = find(vals.begin() + pos2, vals.end(), 0) - vals.begin();
      vals[pos0] = 1;
      pos2 = pos0;
      for (long long i = pos0 - 1; i >= 0; --i) {
        vals[i] = 0;
      }
    }
    long long ans = 0;
    long long pw = 1;
    if (pos2 == -1) {
      ans = num;
    } else {
      for (long long i = pos2; i < vals.size(); ++i) {
        ans += vals[i] * pow(3, i);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
