#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n, sum = 0, prod = 1;
    cin >> n;
    while (sum < n) {
      sum += prod;
      prod *= 3;
    }
    while (prod) {
      if (sum - prod >= n) sum -= prod;
      prod /= 3;
    }
    cout << sum << endl;
  }
}
