import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Ctutest1 {

    static StringTokenizer st = new StringTokenizer("");
    static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    //static BufferedReader input;
    
    static int tbl[] = new int[18];
    static int nodes[] = new int[18];
    static int links[] = new int[18];
    static int tbllen = 16;
    
    static String nextToken() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(input.readLine());
        }
        return st.nextToken();
    }

    static int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }
    
    public static void fillTable() {
        
        nodes[0] = 1;
        links[0] = 0;
        tbl[0] = 1;
        
        for (int i = 0; i < tbllen + 1; i++) {
            nodes[i + 1] = nodes[i] * 3;
            links[i + 1] = links[i] * 3 + nodes[i] * 2;
            tbl[i + 1] = nodes[i + 1] + links[i + 1]; 
        }
    }

    public static void main(String[] args) throws IOException, Exception {
        String line;

        //input = new BufferedReader(new InputStreamReader(new FileInputStream("test.in")));
        while ((line = input.readLine()) != null) {
            st = new StringTokenizer(line);

            int N = nextInt();

            fillTable();

            for (int i = 0; i < N; i++) {
                int X = nextInt();
                
                int filling[] = new int[tbllen];
                int fillen = -1;
                for (int j = tbllen - 1; j >= 0; j--) {
                    filling[j] = 0;
                }
                
                
                for (int j = tbllen - 1; j >= 0; j--) {
                    if (X < nodes[j])
                        continue;
                    
                    if (fillen == -1)
                        fillen = j;
                    
                    while (X >= nodes[j]) {
                        filling[j]++;
                        X -= nodes[j];
                    }
                }
                
                for (int j = fillen; j >= 0; j--) {
                    System.out.print(filling[j]);
                    if (j != 0) {
                        System.out.print(" ");
                    }
                }
                System.out.println();

            }

            //System.out.println(n + " + " + m + " = " + (n + m));
        }
    }

}
