#include using namespace std; const int maxn = 100100; int x[maxn*4],f[maxn],mn[maxn],mx[maxn],a[maxn],ans[maxn]; int ch[maxn][2],c[maxn]; int fnd(int u) { return f[u]==u?u:(f[u]=fnd(f[u])); } void build(int l,int r,int rt) { if (l == r) { x[rt]=0; if (a[l] == a[l+1]) x[rt]=max(x[rt],l); return ; } int mid=(l+r)/2; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); x[rt] = max(x[rt<<1],x[rt<<1|1]); } void upd(int p,int l,int r,int rt) { // printf("upd %d [%d %d]\n",p,l,r); if (l == r) { int p1 = fnd(l), v1 = a[p1]; int p2 = fnd(l+1), v2 = a[p2]; if (p1!=p2 && v1==v2) x[rt]=l; else x[rt]=0; return ; } int mid=(l+r)/2; if (p <= mid) upd(p,l,mid,rt<<1); else upd(p,mid+1,r,rt<<1|1); x[rt] = max(x[rt<<1],x[rt<<1|1]); } int tot,n; void dfs(int u) { tot++; printf("%d%c",u," \n"[tot==n-1]); if (ch[u][0]>ch[u][1]) swap(ch[u][0],ch[u][1]); for (int i=0;i<2;i++) if (ch[u][i]) { dfs(ch[u][i]); } } int main(void) { // freopen("e.in","r",stdin); int T; scanf("%d",&T); while (T--) { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=1;i<=n;i++) f[i]=mn[i]=mx[i]=i,ch[i][0]=ch[i][1]=c[i]=0; build(1,n-1,1); int ok = 1; for (int i=n-1;i>=1;i--) { // printf("i = %d\n",i); if (x[1] == 0) {ok=0; break;} ans[i] = x[1]; int fu = fnd(ans[i]), fv = fnd(ans[i]+1); // printf("x1 = %d\n",x[1]); // printf("fu = %d, fv = %d\n",fu,fv); int p = mn[fu], q = mx[fv]; f[fu]=fv, a[fv]--, mn[fv]=mn[fu]; ch[ans[i]][0] = c[fu]; ch[ans[i]][1] = c[fv]; c[fv] = ans[i]; // printf("rt = %d, mn = %d, mx = %d\n",ans[i],p,q); // printf("upd %d %d %d\n",ans[i],p-1,q); upd(ans[i],1,n-1,1); if (p>1) upd(p-1,1,n-1,1); if (q