#include <bits/stdc++.h>
using namespace std;
long long exponentMod(long long A, long long B) {
  if (A == 0) return 0;
  if (B == 0) return 1;
  long long y;
  if (B % 2 == 0) {
    y = exponentMod(A, B / 2);
    y = (y * y);
  } else {
    y = A;
    y = (y * exponentMod(A, B - 1));
  }
  return y;
}
int main() {
  long long t;
  cin >> t;
  vector<long long> p(40);
  for (int i = 0; i < 40; i++) {
    p[i] = exponentMod(3, i);
  }
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> b(40);
    long long temp = n;
    long long it = 0;
    while (temp > 0) {
      b[it] = temp % 3;
      temp = temp / 3;
      it++;
    }
    long long c = 0;
    long long s = -1;
    long long ma = -1;
    for (long long i = 0; i < 40; i++) {
      if (c != 0 && s == -1) {
        s = i - 1;
        ma = max(s, ma);
      } else if (c == 0)
        s = -1;
      b[i] = b[i] + c;
      c = 0;
      if (b[i] == 2) {
        b[i] = 0;
        c++;
      } else if (b[i] == 3) {
        b[i] = 0;
        c++;
      }
    }
    if (ma != -1) {
      for (long long i = 0; i < ma; i++) {
        b[i] = 0;
      }
    }
    long long ans = 0;
    for (long long i = 0; i < 40; i++) {
      ans = ans + p[i] * (long long)b[i];
    }
    cout << ans << endl;
  }
  return 0;
}
