#include <bits/stdc++.h>
using namespace std;
int mp[40];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int i, tc;
  long long ans, n, a, ans1, val[50];
  val[0] = 1LL;
  for (i = 1; i <= 38; i++) {
    val[i] = 3LL * val[i - 1];
  }
  cin >> tc;
  while (tc--) {
    cin >> n;
    a = n;
    ans = 0LL;
    ans1 = 4000000000000000000LL;
    for (i = 38; i >= 0; i--) {
      if (val[i] > n) {
        ans1 = min(ans1, ans + val[i]);
      } else if (val[i] == n) {
        ans += val[i];
        n = 0;
        break;
      } else {
        mp[i] = 1;
        n -= val[i];
        ans += val[i];
      }
    }
    if (n != 0) ans = ans1;
    cout << ans << "\n";
  }
  return 0;
}
