#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y) {
  long long ans = 1;
  while (y) {
    if (y % 2) ans = ans * x;
    y = y / 2;
    x = x * x;
  }
  return ans;
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v;
    while (n) {
      v.push_back(n % 3);
      n /= 3;
    }
    v.push_back(0);
    for (int i = v.size() - 1; i >= 0; i--) {
      if (v[i] == 2) {
        for (int j = i + 1; j < v.size(); j++) {
          if (v[j] == 0) {
            v[j] = 1;
            for (int k = j - 1; k >= 0; k--) {
              v[k] = 0;
            }
            break;
          }
        }
        break;
      }
    }
    long long ans = 0;
    for (int i = 0; i < v.size(); i++) ans += v[i] * power(3, i);
    cout << ans << endl;
  }
}
