#include <bits/stdc++.h>
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, m, i, j, t1, x, y, t2, sum = 0, z, count = 0;
    long long a[100], p = 1, t3 = 0, t4, t5, t6, t7;
    scanf("%lld", &n);
    y = n;
    sum = 0;
    y = n;
    for (i = 0;; i++) {
      x = pow(3, i);
      sum = sum + x;
      if (sum >= y) break;
    }
    t7 = sum;
    for (j = 0; j < i; j++) {
      x = pow(3, j);
      t2 = sum - x;
      if (t2 >= y) sum = sum - x;
    }
    t5 = sum;
    sum = t7;
    for (j = i; j >= 0; j--) {
      x = pow(3, j);
      t2 = sum - x;
      if (t2 >= y) sum = sum - x;
    }
    t6 = sum;
    if (t6 > t5)
      printf("%lld\n", t5);
    else
      printf("%lld\n", t6);
  }
  return 0;
}
