#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  long long int powers[39];
  powers[0] = 1;
  for (long long int i = 1; i < 39; ++i) {
    powers[i] = powers[i - 1] * 3;
  }
  while (t-- > 0) {
    long long int n;
    cin >> n;
    long long int arr[39] = {0};
    long long int temp = -1;
    long long int rem = n;
    long long int ans = 0;
    for (long long int i = 38; i >= 0; --i) {
      if (powers[i] > rem) {
      } else {
        arr[i] = 1;
        if (temp == -1) {
          temp = i;
        }
        rem -= powers[i];
      }
    }
    if (rem == 0) {
      cout << n << endl;
    } else {
      long long int final_ans = 0;
      long long int flag = 0;
      for (long long int i = 0; i < 39; ++i) {
        if (arr[i] == 1 && flag == 0) {
          while (i < 39 && arr[i] == 1) {
            i++;
          }
          final_ans += powers[i];
          flag = 1;
        } else if (arr[i]) {
          final_ans += powers[i];
        }
      }
      cout << min(final_ans, powers[temp + 1]) << endl;
    }
  }
}
