#include <bits/stdc++.h>
using namespace std;
string f(long long int a) {
  string ans = "";
  long long int b = a;
  while (b >= 1) {
    ans.insert(0, 1, (b % 3) + '0');
    b /= 3;
  }
  return ans;
}
long long int POW(long long int a, long long int b) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  long long int c = POW(a, b / 2);
  c = c * c;
  if (b % 2 == 1) {
    c *= a;
  }
  return c;
}
int main() {
  long long int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    long long int n;
    cin >> n;
    string s = f(n);
    long long int a0 = -1, a2 = -1;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '2') {
        a2 = j;
        break;
      }
    }
    for (int j = a2 - 1; j >= 0; j--) {
      if (s[j] == '0') {
        a0 = j;
        break;
      }
    }
    if (a2 == -1) {
      cout << n << endl;
      continue;
    } else if (a0 == -1) {
      cout << POW(3, s.size()) << endl;
      continue;
    } else {
      s[a0] = '1';
      long long int ans = 0;
      long long int ff = s.size() - a0 - 1;
      for (int j = a0; j >= 0; j--) {
        ans += POW(3, ff) * (s[j] - '0');
        ff++;
      }
      cout << ans << endl;
      continue;
    }
  }
}
