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