#include <iostream>
#include <algorithm>
using namespace std;
char str[10];
int main(){
int t, n;
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= n; ++i)
str[i - 1] = i + '0';
str[n] = '\0';
do{
cout << str << endl;
}while(next_permutation(str, str + n));
}
return 0;
}