#include <bits/stdc++.h>
using namespace std;
long long int poo(long long int, int);
int main() {
  int test;
  cin >> test;
  while (test) {
    long long int num;
    cin >> num;
    for (int i = 1;; i++) {
      int a = 1;
      int count = 0;
      long long int ans = 0;
      while (a <= i) {
        if ((a & i) > 0) {
          ans += poo(3, count);
        }
        a <<= 1;
        count++;
        if (ans >= num) {
          break;
        }
      }
      if (ans >= num) {
        cout << ans << endl;
        break;
      }
    }
    test--;
  }
}
long long int poo(long long int a, int b) {
  int c = a;
  for (int i = 0; i < b - 1; i++) {
    a *= c;
  }
  if (b == 0) {
    a = 1;
  }
  return a;
}
