link
int M,N;
int arr[1001];
void dfs(vector<int>& post, int idx){
if(idx>N) return ;
dfs(post,idx*2);
dfs(post,idx*2+1);
post.push_back(arr[idx]);
}
int main(){
cin>>M>>N;
for(int i=1;i<=M;i++){
for(int j=1;j<=N;j++){
cin>>arr[j];
}
int ismax=0;
for(int j=N/2;j>=1;j--){
int left=2*j;
int right=2*j+1;
if(left<=N && arr[j]>arr[left]){
if(ismax==0) ismax=1;
else if(ismax==-1){
ismax=-2;
break;
}
}
if(left<=N && arr[j]<arr[left]){
if(ismax==0) ismax=-1;
else if(ismax==1){
ismax=-2;
break;
}
}
if(right<=N && arr[j]>arr[right]){
if(ismax==0) ismax=1;
else if(ismax==-1){
ismax=-2;
break;
}
}
if(right<=N && arr[j]<arr[right]){
if(ismax==0) ismax=-1;
else if(ismax==1){
ismax=-2;
break;
}
}
}
if(ismax==-2) printf("Not Heap
");
else if(ismax==-1) printf("Min Heap
");
else printf("Max Heap
");
vector<int> post;
dfs(post,1);
for(int i=0;i<N;i++){
if(i>0) printf(" ");
printf("%d", post[i]);
}
printf("
");
}
return 0;
}