#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long copyN = n;
  vector<long long> v;
  while (n > 0) {
    v.push_back(n % 3);
    n /= 3;
  }
  v.push_back(0);
  reverse(v.begin(), v.end());
  long long last0;
  bool is2 = false;
  for (long long i = 0; i < v.size(); i++) {
    if (v[i] == 0) {
      last0 = i;
    }
    if (v[i] == 2) {
      is2 = true;
      break;
    }
  }
  if (!is2) {
    cout << copyN << endl;
    return;
  }
  long long ans = 0;
  for (long long i = 0; i < v.size(); i++) {
    ans *= 3;
    if (i < last0) ans += v[i];
    if (i == last0) ans++;
  }
  cout << ans << endl;
}
signed main() {
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    solve();
  }
  return 0;
}
