#include <bits/stdc++.h>
using namespace std;
int a[110];
int r;
void solve(int x) {
  r = 0;
  while (x) {
    a[r++] = x % 3;
    x /= 3;
  }
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    memset(a, 0, sizeof a);
    int n;
    scanf("%d", &n);
    solve(n);
    a[r] = 0;
    for (int i = r; i >= 0; i--) {
      if (a[i] == 2) {
        for (int j = i + 1; j <= r; j++) {
          if (a[j] == 1)
            a[j] = 0;
          else if (a[j] == 0) {
            a[j] = 1;
            break;
          }
        }
        for (int k = i; k >= 0; k--) a[k] = 0;
        break;
      }
    }
    int ans = 0;
    for (int i = r; i >= 0; i--) {
      ans += a[i] * pow(3, i);
    }
    printf("%d\n", ans);
  }
}
