#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int q;
  unsigned long long n, m;
  string s;
  cin >> q;
  int tab[70];
  while (q--) {
    cin >> n;
    bool bad = false;
    unsigned long long pow3 = 1;
    m = n;
    while (n > 0) {
      if (n % 3 == 0) {
        if (bad) {
          m -= m % pow3;
          m += pow3;
          bad = false;
        }
      } else if (n % 3 == 2) {
        bad = true;
      }
      pow3 *= 3;
      n /= 3;
    }
    if (bad) {
      m -= m % pow3;
      m += pow3;
      bad = false;
    }
    cout << m << '\n';
  }
}
