import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.regex.Pattern;

public class Lode {

	public static void nthPrice() {
		int a = 17;
		int[] price = new int[a];
		int[] weight = new int[a];
		weight[1] = 1;
		price[1] = 1;

		for (int i = 2; i < a; i++) {
			weight[i] = weight[i - 1] * 3;
			price[i] = 3 * price[i - 1] + weight[i - 1] * 2;
		}

		System.out.println(Arrays.toString(weight));
		System.out.println(Arrays.toString(price));

	}

	private static int[] weights = { 0, 1, 3, 9, 27, 81, 243, 729, 2187, 6561,
			19683, 59049, 177147, 531441, 1594323, 4782969, 14348907 };
	private static int[] prices = { 0, 1, 5, 21, 81, 297, 1053, 3645, 12393,
			41553, 137781, 452709, 1476225, 4782969, 15411789, 49424013, 157837977 };

	public static void main(String[] args) throws IOException {
		BufferedReader buf = new BufferedReader(
				new InputStreamReader(System.in));
		Pattern pattern = Pattern.compile("\\s");
		String line = buf.readLine();
		int n = Integer.parseInt(line);

		for (int j = 0; j < n; j++) {
			line = buf.readLine();
			int cargo = Integer.parseInt(line);
			String result = "";

			for (int i = weights.length - 1; i > 0; i--) {
				int amount = 0;
				while(weights[i] <= cargo){
					amount++;
					cargo-=weights[i];
				}
				result=result + amount;
			}
			result.trim();
			boolean ok = false;
			String newResult = "";
			for (int i = 0; i < result.length(); i++) {
				if(result.charAt(i) != '0'){
					ok = true;
				}
				if(ok){
					newResult = newResult + result.charAt(i) + " ";
				}
			}
			System.out.println(newResult.trim());
		}

	}

}
