#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s = "";
    long long n;
    cin >> n;
    long long a = n;
    while (n != 0) {
      int r = n % 3;
      s = s + char(r + 48);
      n = n / 3;
    }
    s = s + '0';
    int size = s.size();
    int test = 0;
    for (int i = size - 1; i >= 0; i--) {
      if (s[i] == '2') {
        for (int j = i; j >= 0; j--) {
          s[j] = '0';
        }
        int j = i + 1;
        while (s[j] != '0') {
          s[j] = '0';
          j++;
        }
        s[j] = '1';
        test = 1;
        break;
      }
    }
    if (test == 0)
      cout << a << endl;
    else {
      long long ans = 0, p = 1;
      for (long long i = 0; i < s.size(); i++) {
        if (s[i] == '1') {
          ans += p;
        }
        p = p * 3;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
