#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a, b, x = 0, p = 0, k = 0, flag = 0, i, j;
  cin >> n;
  while (n != 0) {
    cin >> m;
    for (i = 0; i <= 9; i++) {
      x = pow(3, i);
      k += x;
      if (k >= m) {
        if (pow(3, i) >= m) {
          if (pow(3, i) == m)
            flag = 1;
          else
            flag = 2;
        }
        break;
      }
    }
    if (flag == 1) {
      cout << pow(3, i) << endl;
    } else {
      for (j = i; j >= 0; j--) {
        int p = pow(3, j);
        k -= p;
        if (k < m) {
          k += p;
        }
      }
      cout << k << endl;
    }
    i = 0;
    k = 0;
    j = 0;
    x = 0;
    flag = 0;
    n--;
  }
}
