#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
long long n;
vector<long long> v;
void init() {
  for (int i = 0; i <= 38; i++) v.push_back(pow(3, i));
}
int main() {
  init();
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    long long res = 0, cur = 0;
    while (res < n) {
      res += v[cur];
      cur++;
    }
    while (cur >= 0) {
      if (res - v[cur] >= n) res -= v[cur];
      cur--;
    }
    cout << res << endl;
  }
  return 0;
}
