
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class lode {

    public static void main(String[] args) throws Exception {
        //doMain(new FileInputStream(new File("testDataLode")));
        doMain(System.in);
    }

    private static void doMain(InputStream is) throws Exception {
        List<List<Integer>> data = new ArrayList<List<Integer>>();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
            if (!line.isEmpty()) {
                LinkedList<Integer> ints = readLine(line);
                data.add(ints);
            }
        }
        reader.close();
        doLode(data.subList(1, data.size()));
    }

    private static LinkedList<Integer> readLine(String s) {
        String[] arr = s.split(" ");

        LinkedList<Integer> list = new LinkedList<Integer>();
        for (String a : arr) {
            if (!a.isEmpty()) {
                list.add(Integer.parseInt(a));
            }
        }

        return list;
    }

    private static void testPrint(Collection<? extends Object> s) {
        for (Object any : s) {
            System.out.println(any);
        }
    }

    private static void doLode(List<List<Integer>> data) {
        for (List<Integer> instance : data) {
            doLodeInstance(instance.get(0));
        }
    }

    private static void doLodeInstance(int in) {
        double size = in;

        double i = Double.valueOf(Math.floor(Math.log(size) / Math.log(3d))).intValue();

        List<Integer> out = new ArrayList<Integer>();


        int count = 0;
        while (i > 0) {
            if (size >= Math.pow(3d, i)) {
                size = size - Math.pow(3d, i);
                count++;
            } else {
                i = i - 1;
                out.add(count);
                count = 0;
            }
        }
        out.add(Double.valueOf(size).intValue());

        StringBuilder sb = new StringBuilder("");
        int c = 0;
        for (Integer outi : out) {
            sb.append(outi);
            if (c != out.size() - 1) {
                sb.append(" ");
            }
            c++;
        }
        System.out.println(sb);
    }

    private static class Min {

        int instanceMin = Integer.MAX_VALUE;

        private int min(int a) {
            instanceMin = Math.min(a, instanceMin);
            return instanceMin;
        }

        private int getMin() {
            return instanceMin;
        }
    }
}
