显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int h[100005];
int v[100005];
int a[100005];
bool in[100005];
void solve(){
memset(v,0,sizeof v);
memset(in,0,sizeof in);memset(a,0,sizeof a);memset(h,0,sizeof h);
int n;cin>>n;
for(int i=1;i<=n-1;i++){
cin>>h[i];
v[h[i]]++;
}
if(h[n-1]!=1){
cout<<-1<<endl;return;
}
int x1=0,x2=0;
for(int i=1;i<=n;i++){
if(v[i]==0){
if(x1)x2=i;
else x1=i;
}if(v[i]>1&&i>1||v[i]>2){
cout<<-1<<endl;return;
}
}
if(x1>x2)swap(x1,x2);
if(x1==0)x1=1;
int l=1,r=n;
a[l]=x1;a[r]=x2;
in[x1]=in[x2]=1;
int nw=1;
for(int i=3;i<=n;i++){
if(a[l]>a[r]){
while(in[h[nw]])nw++;
a[++l]=h[nw];
in[h[nw]]=1;
}else{
while(in[h[nw]])nw++;
a[--r]=h[nw];
in[h[nw]]=1;
}
}
for(int i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
int t;
scanf("%d",&t);
while(t--) solve();
return 0;
}