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