#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t, n, i, p, cnt, ans;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<long long> v;
    while (n) {
      v.push_back(n % 3);
      n /= 3;
    }
    v.push_back(0);
    cnt = 1;
    ans = 0;
    for (i = 0; i < v.size(); i++, cnt *= 3) {
      if (v[i] >= 2) {
        ans = 0;
        v[i + 1]++;
      } else {
        ans += v[i] * cnt;
      }
    }
    cout << ans << '\n';
    v.clear();
  }
  return 0;
}
