#include <bits/stdc++.h>
using namespace std;
long long int q, n;
long long int a, b, ans;
int main() {
  cin >> q;
  while (q--) {
    cin >> n;
    a = 1;
    b = 0;
    while (a < n) {
      a = a * 3;
    }
    ans = a;
    a = a / 3;
    while (a > 0) {
      if (a + b < n) {
        b = b + a;
      } else {
        ans = min(ans, a + b);
      }
      a = a / 3;
    }
    cout << ans << endl;
  }
}
