#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long int t{1};
  cin >> t;
  while (t--) {
    long long int n{};
    cin >> n;
    long long int sum{};
    long long int cp = n;
    vector<long long int> a;
    bool flag{true};
    while (flag == true) {
      if (n % 3 == 2) {
        n += 1;
      }
      a.push_back(n % 3);
      n = n / 3;
      if (n <= 0) {
        flag = false;
      }
    }
    long long int p[a.size() + 1];
    p[0] = 1;
    for (long long int i = 0; i < a.size(); i++) {
      sum += a[i] * p[i];
      p[i + 1] = p[i] * 3;
    }
    for (long long int i = a.size() - 1; i >= 0; i--) {
      if (a[i] == 1) {
        if (sum - p[i] >= cp) {
          sum = sum - p[i];
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}
