#include <bits/stdc++.h>
using namespace std;
int dig[100];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(dig, 0, sizeof dig);
    long long n;
    cin >> n;
    int len;
    long long tem = n;
    for (len = 0; n; len++, n /= 3) dig[len] = n % 3;
    int two = -1;
    for (int i = len - 1; i >= 0; i--) {
      if (dig[i] == 2) {
        two = i;
        break;
      }
    }
    if (two == -1) {
      printf("%lld\n", tem);
      continue;
    }
    int last = two + 1;
    for (last; dig[last]; last++)
      ;
    for (int i = 0; i < last; i++) dig[i] = 0;
    dig[last] = 1;
    long long ans = 0;
    for (int i = max(len, last); i >= 0; i--) ans = ans * 3 + dig[i];
    cout << ans << endl;
  }
  return 0;
}
