#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 2e6 + 10;
int st[N], g[N];
long long power(long long a, long long b) {
  if (b == 0) return 1;
  long long temp = power(a, b / 2);
  temp = temp * temp;
  if (b & 1) temp *= a;
  return temp;
}
void solve(long long n) {
  long long num = n;
  int t = 0, cnt = 0;
  while (num) {
    st[++t] = num % 3;
    num /= 3;
  }
  while (t > 0) {
    g[++cnt] = st[t--];
  }
  int now = 1, pos, flag = 0;
  for (int i = 1; i <= cnt; i++) {
    if (g[i] == 2) {
      pos = i;
      flag = 1;
      break;
    }
  }
  if (!flag) {
    cout << n << endl;
    return;
  }
  long long ans = 0;
  while (g[pos] != 0 && pos >= 1) pos--;
  for (int i = cnt; i > pos; i--) {
    if (g[i] == 1) {
      ans += power(3, cnt - i) << 1;
      g[i - 1]++;
    } else if (g[i] == 2) {
      ans += power(3, cnt - i);
      g[i - 1]++;
    } else if (g[i] == 3) {
      g[i - 1]++;
    }
  }
  cout << n + ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    solve(n);
  }
  return 0;
}
