#include <bits/stdc++.h>
using namespace std;
long long t = 1;
void solve() {
  long long n;
  cin >> n;
  long long t = n;
  vector<long long> v;
  while (t) {
    v.push_back(t % 3);
    t = t / 3;
  }
  reverse(v.begin(), v.end());
  long long key = -1;
  for (long long i = 0; i < v.size(); i++) {
    if (v[i] == 2) {
      key = i;
      break;
    }
  }
  if (key == -1) {
    cout << n << endl;
    return;
  }
  int flag = 0;
  int key2;
  for (long long i = key; i >= 0; i--) {
    if (v[i] == 0) {
      flag = 1;
      v[i] = 1;
      key2 = i;
      break;
    }
  }
  if (flag == 0) {
    cout << (long long)pow(3, v.size()) << endl;
    return;
  }
  long long ans = 0;
  for (long long i = 0; i <= key2; i++) {
    if (v[i] == 1) ans = ans + (long long)pow(3, v.size() - 1 - i);
  }
  cout << ans << endl;
}
int main() {
  int i;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) solve();
  return 0;
}
