#include <bits/stdc++.h>
const int MAXN = 2e2 + 10;
using namespace std;
int ter[MAXN];
long long p[MAXN];
int main() {
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  p[0] = 1;
  for (int i = 1; i < 100; ++i) p[i] = p[i - 1] * 3;
  while (q--) {
    memset(ter, 0, sizeof ter);
    long long n;
    cin >> n;
    long long cur = n;
    int k = 0;
    while (cur) {
      ter[k++] = cur % 3;
      cur /= 3;
    }
    int temp = -1;
    for (int i = 0; i < 100; ++i)
      if (ter[i] == 2) ter[i] = 0, temp = i;
    if (temp != -1)
      for (int i = 0; i < 100; ++i)
        if (ter[i] == 0 && i > temp) {
          ter[i] = 1;
          break;
        } else
          ter[i] = 0;
    long long ans = 0;
    for (int i = 0; i < 100; ++i) ans += ter[i] * p[i];
    cout << ans << endl;
  }
  return 0;
}
