#include <bits/stdc++.h>
using namespace std;
unsigned long long pow3(unsigned long long power) {
  unsigned long long rj = 1;
  while (power > 0) {
    rj = rj * 3;
    power--;
  }
  return rj;
}
unsigned long long log3(unsigned long long x) {
  int rj = 0;
  while ((x / 3) > 0) {
    x = x / 3;
    rj++;
  }
  return rj;
}
unsigned long long good_number(unsigned long long x) {
  bool visited[40];
  memset(visited, 0, sizeof(visited));
  while (x > 0) {
    unsigned long long biggest_power_of_3 = log3(x);
    if (visited[biggest_power_of_3]) {
      for (int i = biggest_power_of_3; i < 40; i++) {
        if (!visited[i]) {
          visited[i] = true;
          break;
        }
        if (visited[i]) visited[i] = false;
      }
      break;
    }
    x = x - pow3(biggest_power_of_3);
    visited[biggest_power_of_3] = true;
  }
  unsigned long long rj = 0;
  for (int i = 0; i < 40; i++) {
    if (visited[i]) {
      rj += pow3(i);
    }
  }
  return rj;
}
void solve() {
  unsigned long long n;
  cin >> n;
  cout << good_number(n) << endl;
  ;
}
int main() {
  int q;
  cin >> q;
  while (q--) solve();
}
