#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
void Solve() {
  long long n;
  cin >> n;
  long long ans = 0, p = 1, m = n;
  while (m > 0) {
    ans += p;
    m -= p;
    p *= 3;
  }
  while (p > 0) {
    if (ans - p >= n) ans -= p;
    p /= 3;
  }
  cout << ans << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) Solve();
  return 0;
}
