import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class A {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
		String line = bfr.readLine();
		int n = Integer.parseInt(line);
		for(int i = 0;i<n;i++)
		{
			int cargo = Integer.parseInt(bfr.readLine());
			int j;
			for(j = 0;;j++)
			{
				if(Math.pow(3, j)>cargo)
					break;
				
			}
			j--;
			
			
			for(;j>=0;j--)
			{
				
				int j3 = (int)Math.pow(3, j);
				int pocet = cargo / j3;
				if(j==0)
					System.out.print(pocet +"");
				else
					System.out.print(pocet +" ");
				cargo = cargo % j3;
			}
			System.out.println("");
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		}

	}

}
