#include <bits/stdc++.h>
using namespace std;
long long int solve_(long long int n) {
  if (n == 0) {
    return -1;
  } else if (n == 1) {
    return 1;
  } else if (n % 3 == 0) {
    return solve_(n / 3);
  } else if (n % 3 == 1) {
    return solve_((n - 1) / 3);
  } else if (n % 3 == 2) {
    return -1;
  }
}
long long int solve(long long int n) {
  if (solve_(n) != -1) {
    return n;
  } else {
    n++;
    int a = 0;
    while (a == 0) {
      if (solve_(n) == -1) {
        n++;
      } else {
        a = -1;
      }
    }
    return n;
  }
}
int main() {
  cin.sync_with_stdio(false);
  long long int t;
  cin >> t;
  while (t-- > 0) {
    long long int n;
    cin >> n;
    long long int ans = solve(n);
    cout << ans << endl;
  }
  return 0;
}
