#include <bits/stdc++.h>
using namespace std;
char reVal(long long int num) {
  if (num >= 0 && num <= 9)
    return (char)(num + '0');
  else
    return (char)(num - 10 + 'A');
}
string fromDeci(long long int n) {
  string res;
  while (n > 0) {
    res += reVal(n % 3);
    n /= 3;
  }
  reverse(res.begin(), res.end());
  return res;
}
long long int val(char c) {
  if (c >= '0' && c <= '9')
    return (long long int)c - '0';
  else
    return (long long int)c - 'A' + 10;
}
long long int toDeci(string str) {
  long long int len = str.size(), power = 1, num = 0, i;
  for (i = len - 1; i >= 0; i--) {
    num += val(str[i]) * power;
    power = power * 3;
  }
  return num;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int g;
  cin >> g;
  while (g--) {
    long long int n, ans, flag = 0, fl = 0, fl1 = 0;
    cin >> n;
    string s = fromDeci(n), t = "1";
    long long int ns = s.size();
    for (long long int i = 0; i < ns; i++)
      if (s[i] == '2') flag++;
    if (flag == 0)
      cout << n << endl;
    else {
      for (long long int i = 0; i < ns; i++) {
        if (s[i] == '2' && fl1 == 0) {
          fl1++;
          for (long long int j = i; j >= 0; j--) {
            if (s[j] == '0') {
              s[j] = '1';
              fl++;
              if (fl)
                for (long long int k = j + 1; k < ns; k++) s[k] = '0';
              i = ns;
              j = -1;
            }
          }
        }
      }
      if (fl)
        cout << toDeci(s) << endl;
      else {
        for (long long int i = 0; i < ns; i++) t += "0";
        cout << toDeci(t) << endl;
      }
    }
  }
  return 0;
}
