#include <bits/stdc++.h>
using namespace std;
int main() {
  long long q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    long long res = 1;
    long long p = 3;
    while (res < n) res += p, p *= 3;
    while (p) {
      if (res - p >= n) res -= p;
      p /= 3;
    }
    cout << res << '\n';
  }
}
