#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD =  998244353;
#define rep(i,a,b) for(ll i=a;i<b;++i) 
#define pii pair<int,int>

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tt=1;
    cin>>tt;
    while(tt--)
    {
        int n;
        cin>>n;
        int a[n];
        rep(i,0,n)
        cin>>a[i];
        vector<int> v;
        rep(i,0,n)
        {
            v.push_back(a[i]);
            int p = i+1;
            rep(j,i+1,n)
            {
                p = j;
                if(a[j]>a[i]) break;
                v.push_back(a[j]);
            }
            reverse(v.begin()+i,v.end());
            i = p-1;
            if(p==n-1 && v.size()==n) i=n;
        }
        for(int i=v.size()-1;i>=0;--i)
        cout<<v[i]<<" ";
        cout<<"\n";
    }
}
    