/*
Given an array of size N. The task is to sort the array elements by completing functions heapify() and buildHeap() which are used to implement Heap Sort.
*/
class Heap_Sort
{
    void printArray(int arr[],int n)
    {
        //int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
    public static void main(String args[])
    {
        Scanner sc  = new Scanner(System.in);
        Heap_Sort hs = new Heap_Sort();
        int arr[] = new int[1000000];
        int T = sc.nextInt();
        while(T>0)
        {
            int n = sc.nextInt();
            for(int i=0;i<n;i++)
                arr[i] = sc.nextInt();
            hs.heapSort(arr,n);
            hs.printArray(arr,n);
            T--;
        }
    }
    public void heapSort(int arr[], int n)
    {
        Sheap g = new Sheap();
        g.buildHeap(arr, n);
        for (int i=n-1; i>=0; i--)
        {
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;
            g.heapify(arr, i, 0);
        }
    }
}


/*This is a function problem.You only need to complete the function given below*/
class Sheap
{
 
     void buildHeap(int arr[], int n)
    {
        // Your code here
        // build heap for half elemetns
        
        // call one by one each elements move first elem to last and heapify 
        // for remaining
        
        for(int i = n/2 - 1 ; i >= 0; --i) {
            
            heapify(arr, n, i );
        }
        
        
        // for(int  i = n-1; i >= 0; --i) {
            
        //     int temp = arr[i];
        //     arr[i] = arr[0];
        //     arr[0] = temp;
            
        //     heapify(arr, i ,0);
            
            
        // }
        
    }
 
    // To heapify a subtree rooted with node i which is
    // an index in arr[]. n is size of heap
    void heapify(int arr[], int n, int i)
    {
        // Your code here
        
        int elem = arr[i];
        int left = i*2 + 1;
        int right = i*2 + 2;
        int largest = i;
        if(left < n && arr[left] > elem ) {
            elem = arr[left];
            largest = left;
        }
        if(right < n && arr[right] > elem ) {
            elem = arr[right];
            largest = right;
        }
        
        if(largest != i) 
        {
            int temp = arr[i];
            arr[i] = arr[largest];
            arr[largest] = temp;
            
            heapify(arr, n, largest);
        }
        
        
        
    }
 }
 
 
