#include <bits/stdc++.h>
using namespace std;
void greaterK(long long n) {
  vector<long long> p;
  long long x = n;
  while (x) {
    p.push_back(x % 3);
    x /= 3;
  }
  int idx = 0;
  for (long long i = 0; i < (long long)p.size() - 1; ++i) {
    if (p[i] >= 2) {
      p[i] = 0;
      p[i + 1]++;
      for (int j = idx; j < i; ++j) {
        p[j] = 0;
      }
      idx = i + 1;
    }
  }
  long long j = (long long)p.size() - 1;
  if (p[j] >= 2) {
    for (auto& i : p) i = 0;
    p.push_back(1);
  }
  long long ans = 0;
  for (long long i = p.size() - 1; i >= 0; --i) {
    ans = ans * 3 + p[i];
  }
  cout << ans << endl;
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n = 29;
    cin >> n;
    greaterK(n);
  }
  return 0;
}
