#include <bits/stdc++.h>
using namespace std;
long long arr[40];
set<int> s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  arr[0] = 1;
  long long i, q, n, last, sum;
  for (i = 1; i <= 38; i++) arr[i] = arr[i - 1] * 3;
  cin >> q;
  while (q--) {
    cin >> n;
    last = 38;
    sum = 0;
    while (last >= 0) {
      for (i = last; i >= 0; i--) {
        if (sum + arr[i] <= n) {
          sum += arr[i];
          last = i - 1;
          s.insert(i);
          break;
        }
      }
      if (sum == n) break;
    }
    for (i = 0; i <= 38; i++) {
      if (s.find(i) == s.end()) break;
    }
    if (sum == n)
      cout << sum << '\n';
    else if (i == s.size())
      cout << arr[i] << '\n';
    else {
      sum += arr[i];
      for (auto it = s.begin(); it != s.end(); it++) {
        if (*it > i) break;
        sum -= arr[*it];
      }
      cout << sum << '\n';
    }
    s.clear();
  }
  return 0;
}
