#include <bits/stdc++.h>
using namespace std;
long long q, n;
int main() {
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n;
    vector<long long> nums;
    long long cSum = 0, power = 0;
    while (cSum < n) {
      cSum += pow(3, power);
      nums.push_back(pow(3, power));
      power++;
    }
    for (long long j = nums.size() - 1; j >= 0; j--) {
      if (cSum - nums[j] >= n) cSum -= nums[j];
    }
    cout << cSum << "\n";
  }
}
