#include <bits/stdc++.h>
using namespace std;
inline void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
const int nmax = 1e6 + 7;
const long long LINF = 1e17;
string to_str(long long x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
int to_num(string str) {
  stringstream ss(str);
  int x;
  ss >> x;
  return x;
}
vector<int> v;
char reVal(int num) {
  if (num >= 0 && num <= 9)
    return (char)(num + '0');
  else
    return (char)(num - 10 + 'A');
}
string fromDeci(string res, long long base, long long inputNum) {
  int index = 0;
  while (inputNum > 0) {
    res += reVal(inputNum % base);
    inputNum /= base;
  }
  reverse(res.begin(), res.end());
  return res;
}
long long val(char c) {
  if (c >= '0' && c <= '9')
    return (long long)c - '0';
  else
    return (long long)c - 'A' + 10;
}
long long toDeci(string str, int base) {
  int len = str.size();
  long long power = 1;
  long long num = 0;
  int i;
  for (i = len - 1; i >= 0; i--) {
    num += val(str[i]) * power;
    power = power * base;
  }
  return num;
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    long long num;
    long long base = 3;
    cin >> num;
    string s = "";
    s = fromDeci(s, base, num);
    int len = s.size();
    int pos = -1;
    for (int i = 0; i < len; i++) {
      if (s[i] == '2') {
        pos = i;
        break;
      }
    }
    if (pos != -1) {
      for (int i = pos; i < len; i++) s[i] = '2';
      for (int i = pos - 1; i >= 0; i--) {
        if (pos < 0) break;
        if (s[i] == '0') {
          break;
        } else
          s[i] = '2';
      }
      long long neww = toDeci(s, 3);
      long long ans = neww + 1;
      cout << ans << endl;
    } else {
      long long neww = toDeci(s, 3);
      cout << neww << endl;
    }
  }
  return 0;
}
