#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int max = 40;
  vector<int> digits(max);
  long long max_val = 1;
  for (int i = 0; i < max - 1; ++i) {
    max_val *= 3;
  }
  for (int tt = 0; tt < t; ++tt) {
    long long n;
    cin >> n;
    fill(digits.begin(), digits.end(), 0);
    long long m = n;
    long long val = max_val;
    int power = max - 1;
    while (m > 0 && power >= 0) {
      if (m >= val) {
        ++digits[power];
        m -= val;
      } else {
        val /= 3;
        --power;
      }
    }
    int first2 = -1;
    for (int i = max - 1; i >= 0; --i) {
      if (digits[i] == 2) {
        first2 = i;
        break;
      }
    }
    if (first2 == -1) {
      cout << n;
    } else {
      int next0 = -1;
      for (int i = first2 + 1; i < max; ++i) {
        if (digits[i] == 0) {
          next0 = i;
          break;
        }
      }
      if (next0 == -1) exit(1);
      digits[next0] = 1;
      val = 1;
      for (int i = 0; i < next0; ++i) {
        val *= 3;
      }
      m = 0;
      for (int i = next0; i < max; ++i) {
        m += digits[i] * val;
        val *= 3;
      }
      cout << m;
    }
    cout << "\n";
  }
  return 0;
}
