#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long int n;
    cin >> n;
    long long int sum = 0LL, temp = 1, i;
    for (i = 0LL;; i++) {
      sum += temp;
      temp = temp * 3;
      if (sum >= n) break;
    }
    for (i = 50LL; i >= 0LL; i--) {
      if (sum - temp >= n) {
        sum = sum - temp;
      }
      temp = temp / 3;
    }
    cout << sum << endl;
  }
  return 0;
}
