#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000000 + 7;
const long long int N = 10000000 + 6;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    long long n;
    cin >> n;
    vector<int> vals;
    int pos2 = -1;
    while (n > 0) {
      vals.push_back(n % 3);
      if (vals.back() == 2) pos2 = int(vals.size()) - 1;
      n /= 3;
    }
    vals.push_back(0);
    if (pos2 != -1) {
      int pos0 = find(vals.begin() + pos2, vals.end(), 0) - vals.begin();
      vals[pos0] = 1;
      for (int i = pos0 - 1; i >= 0; --i) {
        vals[i] = 0;
      }
    }
    long long ans = 0;
    long long pw = 1;
    for (int i = 0; i < int(vals.size()); ++i) {
      ans += pw * vals[i];
      pw *= 3;
    }
    if (vals.back() == 1) ans = pw / 3;
    cout << ans << endl;
  }
  return 0;
}
