#include <bits/stdc++.h>
using namespace std;
int p[60], T;
int main() {
  scanf("%d", &T);
  while (T--) {
    memset(p, 0, sizeof(p));
    long long n, pos = 0, sum = 0, P = 1;
    scanf("%lld", &n);
    while (n) p[pos++] = n % 3, n /= 3;
    int k = pos - 1;
    while (k && p[k] != 2) k--;
    for (int i = k - 1; i >= 0; i--) p[i] = 0;
    while (p[k] == 2) p[k] = 0, p[++k]++;
    pos = max(pos, k + 1LL);
    for (int i = 0; i < pos; i++) {
      if (p[i] == 1) sum += P;
      P *= 3;
    }
    printf("%lld\n", sum);
  }
  return 0;
}
