#include <bits/stdc++.h>
using namespace std;
vector<long long> a;
void el() {
  long long ans = 1;
  for (int i = 0; i < 40; i++) {
    a.push_back(ans);
    ans *= 3;
  }
}
void solve() {
  long long n;
  cin >> n;
  long long temp = n;
  vector<int> tno;
  while (1) {
    if (temp < 3) {
      tno.push_back(temp);
      break;
    }
    tno.push_back(temp % 3);
    temp /= 3;
  }
  tno.push_back(0);
  int flag = 0;
  reverse(tno.begin(), tno.end());
  int len = tno.size();
  int lz = len, l2 = 0;
  for (int i = 0; i != len; i++) {
    if (tno[i] == 0) lz = i;
    if (tno[i] == 2) {
      flag = 1;
      break;
    }
  }
  if (flag)
    for (int i = 0; i != len; i++) {
      if (i == lz) tno[i] = 1;
      if (i > lz) tno[i] = 0;
    }
  reverse(tno.begin(), tno.end());
  long long sum = 0;
  int i = 0;
  for (auto it = tno.begin(); it != tno.end(); it++) {
    if (*it) sum += a[i];
    i++;
  }
  cout << sum << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  el();
  long long test;
  cin >> test;
  while (test--) solve();
  return 0;
}
