#include<stdio.h>
#include<string.h>
int main()
{
int s;
scanf("%d",&s);
while(s--)
{
int n,i,j,t,k,h,sum,m,a[12];
scanf("%d",&n);
memset(a,0,sizeof(a));
sum=1;
for(i=1;i<=n;i++)
{
sum*=i;
a[i]=i;
printf("%d",i);
}
printf("\n");
sum--;
while(sum--)
{
m=0;
for(i=n;i>1;i--)
{
if(a[i]>a[i-1])
{
h=i;
for(j=n;j>=h;j--)
if(a[j]>a[h-1])
{
m=1;
break;
}
t=a[j];a[j]=a[h-1];a[h-1]=t;
}
if(m==1) break;
}
for(j=h+1;j<=n+h;j++)
for(k=h;k<=n+h-j;k++)
if(a[k]>a[k+1]){t=a[k];a[k]=a[k+1];a[k+1]=t;}
for(j=1;j<=n;j++)
printf("%d",a[j]);
printf("\n");
}
}
return 0;
}
//另一种比较方法
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int main()
{
int s;
scanf("%d",&s);
while(s--)
{
int n,i,j,t,k,h,sum,m,f,a[12];
scanf("%d",&n);
memset(a,0,sizeof(a));
sum=1;
for(i=1;i<=n;i++)
{
sum*=i;
a[i]=i;
printf("%d",i);
}
printf("\n");
for(f=1;f<sum;f++)
{
m=0;
for(i=n;i>1;i--)
{
if(a[i]>a[i-1])
{
h=i;
for(j=n;j>=h;j--)
if(a[j]>a[h-1])
{
m=1;
break;
}
t=a[j];a[j]=a[h-1];a[h-1]=t;
}
if(m==1) break;
}
qsort(a+h,n-h+1,sizeof(a[0]),cmp);
/* for(j=h+1;j<=n+h;j++)
for(k=h;k<=n+h-j;k++)
if(a[k]>a[k+1]){t=a[k];a[k]=a[k+1];a[k+1]=t;}*/
for(j=1;j<=n;j++)
printf("%d",a[j]);
printf("\n");
}
}
return 0;
}