#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = 0x3f3f3f3f3f3f3f;
vector<long long int> p;
int main() {
  long long int b = 1;
  while (b <= 1e18 && b > 0) {
    p.push_back(b);
    b *= 3ll;
  }
  int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    vector<int> fac;
    while (n != 0) {
      fac.push_back(n % 3ll);
      n /= 3ll;
    }
    int l0 = -1;
    bool cagou = false;
    for (int i = fac.size() - 1; i >= 0; i--) {
      if (fac[i] == 0)
        l0 = i;
      else if (fac[i] == 2) {
        if (l0 == -1) {
          cagou = true;
          break;
        } else {
          for (int j = 0; j < l0; j++) {
            fac[j] = 0;
          }
          fac[l0] = 1;
          break;
        }
      }
    }
    if (cagou) {
      if (p.size() <= fac.size()) {
        cout << "1350851717672992089" << endl;
      } else
        cout << p[fac.size()] << endl;
    } else {
      long long int ans = 0, bb = 1;
      for (auto b : fac) {
        ans += bb * (long long int)b;
        bb *= 3ll;
      }
      cout << ans << endl;
    }
  }
}
