#include <bits/stdc++.h>
using namespace std;
namespace patch {
template <typename T>
std::string to_string(const T& n) {
  std::ostringstream stm;
  stm << n;
  return stm.str();
}
}  // namespace patch
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }
bool isAPrime(long long m) {
  for (long long p = 2; p * p <= m; p++) {
    if (m % p == 0) {
      return false;
    }
  }
  return true;
}
bool cmp(int a, int b) { return a > b; }
long long binpow(long long a, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1) * a);
  else {
    long long b = binpow(a, n / 2);
    return b * b;
  }
}
bool isF = true;
int e[200001][2];
int cnt[200001];
int component = 0;
void dfs(int v, int current) {
  if (isF) {
    int next = e[v][0];
    e[v][1] = component;
    cnt[component]++;
    isF = false;
    dfs(next, current);
  } else {
    if (current == v) return;
    int next = e[v][0];
    e[v][1] = component;
    cnt[component]++;
    isF = false;
    dfs(next, current);
  }
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long x;
    cin >> x;
    long long x1 = x;
    vector<short> a;
    while (x > 0) {
      a.push_back(x % 3);
      x /= 3;
    }
    a.push_back(0);
    reverse(a.begin(), a.end());
    bool was2 = false;
    int ix = -1;
    for (int i = 1; i < a.size(); i++) {
      if (a[i] == 2) {
        was2 = true;
        ix = i;
        break;
      }
    }
    if (!was2)
      cout << x1;
    else {
      for (int i = ix - 1; i >= 0; i--) {
        if (a[i] == 0) {
          a[i] = 1;
          for (int j = i + 1; j < a.size(); j++) {
            a[j] = 0;
          }
          break;
        }
      }
      unsigned long long ans = 0;
      for (int i = 0; i < a.size(); i++) {
        ans = ans * 3 + a[i];
      }
      cout << ans;
    }
    cout << endl;
  }
  return 0;
}
