#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2010;
const long long mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q, i, j, k, n;
  cin >> q;
  while (q--) {
    cin >> n;
    vector<long long> ter;
    long long t = n;
    while (n > 0) {
      ter.push_back(n % 3);
      n /= 3;
    }
    reverse(ter.begin(), ter.end());
    long long f = 1, ind;
    for (i = 0; i < ter.size(); ++i) {
      if (ter[i] == 2) {
        ind = i;
        f = 0;
        break;
      }
    }
    if (f)
      cout << t << "\n";
    else {
      f = 1;
      for (i = ind; i >= 0; --i) {
        if (ter[i] == 0) {
          ter[i] = 1;
          for (j = i + 1; j < (long long)ter.size(); ++j) ter[j] = 0;
          f = 0;
          break;
        }
      }
      long long ans = 0;
      if (f)
        ans = (long long)pow(3, (long long)ter.size());
      else {
        long long pw = 1;
        for (i = (long long)ter.size() - 1; i >= 0; --i) {
          ans += ter[i] * pw;
          pw *= 3;
        }
      }
      cout << ans << "\n";
    }
  }
}
