#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vvi = vector<vector<int>>;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &e : a) {
        cin >> e;
    }
    vector<int> mx(n);
    mx[0] = a[0];
    for (int i = 1; i < n; ++i) {
        mx[i] = max(mx[i - 1], a[i]);
    }
    vector<int> ans;
    ans.reserve(n);
    int r = n;
    for (int i = n - 1; i >= 0; --i) {
        if (!i || mx[i] != mx[i - 1]) {
            copy(begin(a) + i, begin(a) + r, back_inserter(ans));
            r = i;
        }
    }
    for (auto e : ans) {
        cout << e << ' ';
    }
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}
