#include <bits/stdc++.h>
using namespace std;
long long int arr[1000][1000];
int main() {
  int t;
  cin >> t;
  vector<long long int> v;
  long long int pro = 1;
  while (pro <= 1e18) {
    v.push_back(pro);
    pro *= 3;
  }
  while (t--) {
    long long int n;
    cin >> n;
    long long int res = v[v.size() - 1];
    long long int sum = 0, prev = 0;
    for (long long int i = v.size() - 2; i >= 0; i--) {
      if (sum + v[i] == n) {
        res = n;
        break;
      } else if (sum + v[i] > n) {
        long long int val = sum + v[i];
        res = min(res, val);
      } else {
        sum += v[i];
      }
    }
    cout << res << endl;
  }
}
