显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N],p[N],t,n,v[N];
void init(){
memset(p,0,sizeof(p));
memset(v,0,sizeof(v));
}
int main(){
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
for(int i=1;i<n;i++){
scanf("%d",&a[i]);
v[a[i]]++;
}
int flag=0;
for(int i=2;i<=n;i++){
if(v[i]>1)flag=1;
}
if(a[n-1]!=1){
printf("-1\n");
}else if(v[1]>2){
printf("-1\n");
}else if(flag){
printf("-1\n");
}else if(v[1]==1){
p[1]=1;
int op;
for(int j=1;j<=n;j++){
if(v[j]==0)op=j;
}
p[n]=op;
int l=2,r=n-1,idx=1;
while(l<r){
if(p[l-1]>p[r+1]){
p[l++]=a[idx];
}else{
p[r--]=a[idx];
}
idx++;
}
if(!p[l])p[l]=a[idx];
for(int i=1;i<=n;i++){
printf("%d ",p[i]);
}
printf("\n");
}else if(v[1]==2){
int boo=1;
// cout<<endl;
for(int j=1;j<=n;j++){
// cout<<v[j]<<" ";
if(v[j]==0&&boo){
p[1]=j;
boo=0;
}else if(v[j]==0){
p[n]=j;
}
}
// cout<<endl;
if(p[1]>p[n])swap(p[1],p[n]);
// cout<<p[1]<<' '<<p[n]<<"RRR"<<endl;
// cout<<v[p[1]]<<" "<<v[p[n]]<<"TTT"<<endl;
int l=2,r=n-1,idx=1;
while(l<r){
if(p[l-1]>p[r+1]){
p[l++]=a[idx];
}else{
p[r--]=a[idx];
}
idx++;
}
p[l]=a[idx];
for(int i=1;i<=n;i++){
printf("%d ",p[i]);
}
printf("\n");
}else if(v[1]==0){
printf("-1\n");
}
}
return 0;
}