#include <bits/stdc++.h>
using namespace std;
vector<int> g[1000005];
void init() {
  for (int i = 0; i < 1000005; i++) {
    g[i].clear();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int x = n;
    string a = "";
    while (x > 0) {
      int r = x % 3;
      a = (char)(r + '0') + a;
      x /= 3;
    }
    long long int ans = 0;
    string res = "";
    int c = 0;
    int p = n + 5;
    for (int i = a.length() - 1; i >= 0; i--) {
      if (a[i] == '0' and c == 0)
        res = '0' + res;
      else if (a[i] == '0' and c == 1) {
        res = '1' + res;
        c = 0;
        p = i;
      } else if (a[i] == '1' and c == 0)
        res = '1' + res;
      else if (a[i] == '1' and c == 1) {
        res = '0' + res;
        c = 1;
      } else if (a[i] == '2' and c == 0) {
        res = '0' + res;
        c = 1;
      } else if (a[i] == '2' and c == 1) {
        res = '0' + res;
        c = 1;
      }
    }
    for (int i = p + 1; i < res.length(); i++) res[i] = '0';
    if (c == 1) {
      string ans = "1";
      for (int i = 0; i < res.length(); ++i) {
        ans += '0';
      }
      res = ans;
    }
    for (int i = 0; i < res.length(); ++i) {
      ans = (ans * 3) + (int)(res[i] - '0');
    }
    cout << ans << endl;
  }
}
