#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
int main() {
  int q;
  scanf("%d", &q);
  ;
  while (q--) {
    int n;
    vector<int> rep;
    scanf("%d", &n);
    ;
    while (n) {
      rep.push_back(n % 3);
      ;
      n /= 3;
    }
    for (int i = rep.size() - 1; i >= 0; i--) {
      if (rep[i] == 2) {
        int best = rep.size();
        for (int j = i + 1; j < rep.size(); j++) {
          if (rep[j] == 0) {
            best = j;
            break;
          }
        }
        for (int i = 0; i < best; i++) {
          rep[i] = 0;
        }
        if (best < rep.size())
          rep[best] = 1;
        else
          rep.push_back(1);
        ;
        break;
      }
    }
    int ans = 0;
    for (int i = rep.size() - 1; i >= 0; i--) ans = ans * 3 + rep[i];
    printf("%d\n", ans);
    ;
  }
  return 0;
}
