#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long double PI = 4 * atan(1);
long long q, n, a[41], ans, x, cur, po[40];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> q;
  x = 3;
  po[0] = 1;
  for (int i = 1; i < 40; i++) {
    po[i] = x;
    x = x * 3LL;
  }
  while (q--) {
    cin >> n;
    ans = 0, cur = 0;
    x = n;
    for (int i = 0; i < 41; i++) a[i] = 0;
    for (long long i = 38; i >= 0; i--) {
      if (po[i] <= n) {
        cur += po[i];
        n -= po[i];
        a[i] = 1;
      }
    }
    if (cur == x) {
      cout << cur << "\n";
      continue;
    }
    for (int i = 0; i < 39; i++) {
      if (a[i] == 0) {
        a[i] = 1;
        break;
      }
      a[i] = 0;
    }
    for (long long i = 0; i <= 38; i++) {
      if (a[i]) ans += po[i];
    }
    cout << ans << "\n";
  }
  return 0;
}
