Leaders in an array
/*
http://ideone.com/lxC3oU
http://www.practice.geeksforgeeks.org/editorial.php?pid=623
http://www.practice.geeksforgeeks.org/problem-page.php?pid=623
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int t, n;
cin >> t;
while(t--){
cin >> n;
int a[101], temp[101];
for(int i=0; i<n; i++)
cin >> a[i];
int max = a[n-1], k=0;
temp[k++] = max;
for(int i=n-2; i>=0; i--){
if(a[i] > max){
max = a[i];
temp[k++] = max;
}
}
for(int i=k-1; i>=0; i--){
cout << temp[i];
cout << ((i==0) ? "\n" : " ");
}
}
return 0;
}