#include <bits/stdc++.h>
using namespace std;
bool base3(long long n) {
  bool check = true;
  while (n > 0) {
    if (n % 3 == 2) {
      check = false;
      break;
    }
    n /= 3;
  }
  if (check)
    return true;
  else
    return false;
}
int main() {
  long long T, n, k;
  cin >> T;
  for (int i = 1; i <= T; i++) {
    cin >> n;
    if (n >= 1000000000000000000) {
      cout << "1350851717672992089" << endl;
      continue;
    }
    long long m = n;
    while (1) {
      long long k = 1, h = m;
      bool check = true;
      while (h > 0) {
        if (h % 3 == 2) {
          check = false;
          break;
        }
        h /= 3;
        k++;
      }
      if (check)
        break;
      else {
        long long r = 1;
        for (int i = 1; i <= k - 2; i++) r *= 3;
        m += r;
      }
    }
    cout << m << endl;
  }
  return 0;
}
