#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int A[40], i;
  A[0] = 1;
  for (i = 1; i <= 39; i++) A[i] = A[i - 1] * 3;
  int q;
  cin >> q;
  while (q--) {
    long long int n, i, cnt, k = 0, r;
    cin >> n;
    for (i = 0; i <= 39; i++) {
      k = k + A[i];
      if (k >= n) {
        cnt = i;
        break;
      }
    }
    while (cnt >= 0) {
      r = A[cnt];
      if (k - r >= n) {
        k -= r;
      }
      cnt--;
    }
    cout << k << endl;
  }
}
