#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, temp;
    cin >> n;
    temp = n;
    vector<long long> a;
    while (temp != 0) {
      a.push_back(temp % 3);
      temp /= 3;
    }
    long long ad = 0, sz = a.size(), pos = -1;
    for (long long i = 0; i < sz; i++) {
      if (a[i] >= 3) {
        pos = i;
        if (i + 1 < sz) {
          a[i + 1] += a[i] / 3;
        } else {
          a.push_back(a[i] / 3);
        }
        a[i] %= 3;
      }
      if (a[i] % 3 == 2) {
        pos = i;
        ad += pow(3, i);
        if (i + 1 < sz) {
          a[i + 1] += ((a[i] / 3) + 1);
        } else {
          a.push_back((a[i] / 3) + 1);
        }
      }
    }
    sz = a.size(), temp = 0;
    if (pos == -1) {
      cout << n + ad << '\n';
      continue;
    }
    for (long long i = 0; i < sz; i++) {
      if (i == pos + 1) {
        temp += pow(3, i);
      } else if (i > pos && a[i] == 1) {
        temp += pow(3, i);
      }
    }
    cout << temp << '\n';
  }
}
