#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
long long n, m;
long long p[N], s[N], sz[N], ans[N];
vector<long long> v;
int main(void) {
  long long t;
  cin >> t;
  while (t--) {
    v.clear();
    long long n, x;
    cin >> n;
    x = n;
    while (n) v.push_back(n % 3), n /= 3;
    int num = v.size(), flag = -1;
    for (int i = num - 1; i >= 0; i--) {
      if (v[i] == 2) {
        flag = i;
        break;
      }
    }
    if (flag == -1)
      cout << x << endl;
    else {
      int ff = -1;
      for (int i = flag; i < num; i++) {
        if (v[i] == 0) {
          ff = i;
          break;
        }
      }
      if (ff == -1) ff = num;
      long long p = 1, ans = 0;
      for (int i = 0; i < num; i++) {
        if (i >= ff && (v[i] & 1)) ans += p;
        if (i == ff) ans += p;
        p *= 3;
      }
      if (ff == num) ans += p;
      cout << ans << endl;
    }
  }
  return 0;
}
