#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    while (1) {
      bool flag = true;
      int a = n;
      while (a) {
        if (flag && (a % 3 == 2)) {
          flag = false;
        }
        a /= 3;
      }
      if (flag) {
        break;
      }
      ++n;
    }
    cout << n << "\n";
  }
  return 0;
}
