#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 1e9 + 1;
const double eps = 1e-15;
const double EPS = 1e-9;
const double PI = acos(-1.0);
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  int n, t;
  cin >> t;
  while (t--) {
    cin >> n;
    long long int temp = 1;
    while (temp <= n) {
      if (n % (temp * 3) / temp == 2) {
        n = n / temp * temp + temp;
      }
      temp *= 3;
    }
    cout << n << "\n";
    cerr << "---------\n";
  }
  return 0;
}
