#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, x, i = 0, z = 1;
    cin >> n;
    long long a[500] = {0};
    x = n;
    while (x > 0) {
      a[i] = x % 3;
      x = x / 3;
      if (a[i] == 2) z = 2;
      i++;
    }
    a[i] = 0;
    if (z == 1)
      cout << n << "\n";
    else {
      long long j = i - 1, p;
      while (a[j] != 2) j--;
      p = j + 1;
      while (j >= 0) {
        a[j] = 0;
        j--;
      }
      while (p <= i) {
        if (a[p] == 0) {
          a[p] = 1;
          break;
        }
        a[p] = 0;
        p++;
      }
      j = 1;
      long long ans = 0;
      for (p = 0; p <= i; p++) {
        ans = ans + a[p] * j;
        j = j * 3;
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
