#include <bits/stdc++.h>
using namespace std;
long long int mod1 = 998244353;
long long int mod = 1e9 + 7;
bool comp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
long long int a[200005];
void solve() {
  long long int n;
  cin >> n;
  vector<int> t;
  while (n) {
    t.push_back(n % 3);
    n /= 3;
  }
  long long int ans = 0;
  int l = t.size();
  for (int i = t.size() - 1; i >= 0; --i) {
    if (t[i] == 2) {
      for (int j = i + 1; j < l; ++j) {
        ans -= a[j];
      }
      ans += a[l];
      break;
    } else if (t[i]) {
      ans += a[i];
    } else {
      l = i;
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  a[0] = 1;
  for (int i = 1; i <= 39; ++i) a[i] = a[i - 1] * 3;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
