#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, i, e, f, g, n, m, k, l, A[200005], B[200005], t, p, w, j,
    fix[200005];
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    l = 0;
    while (true) {
      m = n + l;
      l++;
      k = 0;
      while (m > 0) {
        if (m % 3 == 2) {
          k = 1;
          break;
        }
        m /= 3;
      }
      if (k == 0) {
        cout << n + l - 1 << endl;
        break;
      }
    }
  }
}
