#include <bits/stdc++.h>
using namespace std;
long long int inf(10e16);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n, power(0), num = 0, carry = 0;
    cin >> n;
    map<long long int, long long int> factors;
    while (n) {
      factors[power] += n % 3;
      power++;
      n /= 3;
    }
    power = 1;
    for (auto x : factors) {
      long long int rep = carry + x.second;
      if (rep >= 2) {
        carry = 1;
        num = 0;
      } else if (rep) {
        carry = 0;
        num += power;
      }
      power *= 3;
    }
    if (carry) num += power;
    cout << num << "\n";
  }
  return 0;
}
