#include <bits/stdc++.h>
using namespace std;
int nr[100], nrquiz;
long long x;
int main() {
  cin >> nrquiz;
  while (nrquiz--) {
    cin >> x;
    int cnt = 0;
    long long aux = x;
    memset(nr, 0, sizeof(nr));
    while (x > 0) {
      nr[cnt++] = x % 3;
      x /= 3;
    }
    long long ans = 0, p = 1;
    int last = -1;
    for (int i = 0; i < cnt; ++i) {
      if (nr[i] == 3) {
        nr[i + 1]++;
        nr[i] = 0;
        last = i;
      } else if (nr[i] == 2) {
        nr[i + 1]++;
        nr[i] = 0;
        last = i;
      }
    }
    for (int i = last; i >= 0; --i) nr[i] = 0;
    for (int i = 0; i <= cnt; ++i) ans += p * nr[i], p *= 3;
    cout << ans << '\n';
  }
  return 0;
}
