#include <bits/stdc++.h>
using namespace std;
string trin(long long int a) {
  string s;
  while (a > 0) {
    char c = '0' + (a % 3);
    string cs = "";
    cs += c;
    s.insert(0, cs);
    a /= 3;
  }
  return s;
}
int main() {
  int mar;
  cin >> mar;
  while (mar--) {
    long long int n;
    cin >> n;
    string s = trin(n);
    int pos = -1;
    bool cp = 0;
    for (int i = s.size() - 1; i >= 0; i--) {
      if (s[i] == '0')
        pos = max(i, pos);
      else if (s[i] == '2') {
        pos = -1;
        cp = 1;
      }
    }
    if (cp == 0) {
      cout << n << '\n';
      continue;
    }
    if (pos == -1) {
      cout << (long long int)pow(3, s.size()) << '\n';
    } else {
      long long int sol = 0;
      s[pos] = '1';
      for (int i = pos + 1; i < s.size(); i++) s[i] = '0';
      for (int i = 0; i < s.size(); i++)
        sol += pow(3, s.size() - i - 1) * (long long int)(s[i] - '0');
      cout << sol << endl;
    }
  }
  return 0;
}
