#include <bits/stdc++.h>
using namespace std;
int q;
long long n;
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%lld", &n);
    vector<int> ter;
    while (n) {
      ter.push_back(n % 3);
      n /= 3;
    }
    ter.push_back(0);
    bool two = false;
    long long x = 1, p = 0;
    for (int& t : ter) {
      if (two && t == 0) p = x, two = false;
      if (t == 2) t = 0, two = true;
      x *= 3;
    }
    x = 1;
    for (int& t : ter) {
      if (x >= p) n += t * x;
      x *= 3;
    }
    printf("%lld\n", n + p);
  }
  return 0;
}
