#include <bits/stdc++.h>
short a[105];
int main(void) {
  int t;
  long long n;
  std::cin >> t;
  while (t--) {
    for (register int i = 0; i < 105; ++i) a[i] = 0;
    std::cin >> n;
    for (register int i = 0; i < 105; ++i) {
      a[i] = n % 3;
      n = n / 3;
    }
    bool flag = 1;
    for (register int i = 0; i < 105; ++i)
      if (a[i] == 2) flag = 0;
    if (flag) {
      n = 0;
      for (register int i = 104; i >= 0; --i) n = n * 3 + a[i];
      std::cout << n << std::endl;
    } else {
      for (register int i = 104; i >= 0; --i)
        if (a[i] == 2) {
          for (register int j = i; j >= 0; --j) a[j] = 0;
          int now = i + 1;
          a[now]++;
          while (a[now] == 2) {
            a[now] = 0;
            a[now + 1]++;
            now++;
          }
          break;
        }
      n = 0;
      for (register int i = 104; i >= 0; --i) n = n * 3 + a[i];
      std::cout << n << std::endl;
    }
  }
  return 0;
}
