#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void File( )
{
freopen ( "in.txt" ,"r" ,stdin ) ;
freopen ( "out.txt" ,"w" ,stdout ) ;
}
string s,ans= "" ;
ll cnt = 0 ,I;
void BT( int i) ;
int main( )
{
ios_base:: sync_with_stdio ( false ) ;
cin .tie ( 0 ) ;
cout .tie ( 0 ) ;
File( ) ;
int T;
cin >> T;
while ( T-- )
{
cin >> s>> I;
BT( 0 ) ;
ans.clear ( ) ;
cnt = 0 ;
}
return 0 ;
}
void BT( int i)
{
if ( cnt == I) return ;
if ( ans.size ( ) == s.size ( ) )
{
cnt++ ;
if ( cnt == I) cout << ans<< endl;
return ;
}
for ( int j= 0 ; j<= ans.size ( ) ; ++ j)
{
string x= "" ;
x+ = s[ i] ;
ans.insert ( j,x) ;
BT( i+ 1 ) ;
ans.erase ( j,1 ) ;
if ( cnt == I) return ;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnZvaWQgRmlsZSgpCnsKICAgIGZyZW9wZW4oImluLnR4dCIsInIiLHN0ZGluKTsKICAgIGZyZW9wZW4oIm91dC50eHQiLCJ3IixzdGRvdXQpOwp9CnN0cmluZyBzLGFucz0iIjsKbGwgY250ID0gMCxJOwp2b2lkIEJUKGludCBpKTsKaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoMCk7CiAgICBjb3V0LnRpZSgwKTsKICAgIEZpbGUoKTsKICAgIGludCBUOwogICAgY2luPj5UOwogICAgd2hpbGUoVC0tKQogICAgewogICAgICAgIGNpbj4+cz4+STsKICAgICAgICBCVCgwKTsKICAgICAgICBhbnMuY2xlYXIoKTsKICAgICAgICBjbnQgPSAwOwogICAgfQogICAgcmV0dXJuIDA7Cn0Kdm9pZCBCVChpbnQgaSkKewogICAgaWYoY250ID09IEkpcmV0dXJuOwogICAgaWYoYW5zLnNpemUoKT09cy5zaXplKCkpCiAgICB7CiAgICAgICAgY250Kys7CiAgICAgICAgaWYoY250ID09IEkpY291dDw8YW5zPDxlbmRsOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGZvcihpbnQgaj0wO2o8PWFucy5zaXplKCk7KytqKQogICAgewogICAgICAgIHN0cmluZyB4PSIiOwogICAgICAgIHgrPXNbaV07CiAgICAgICAgYW5zLmluc2VydChqLHgpOwogICAgICAgIEJUKGkrMSk7CiAgICAgICAgYW5zLmVyYXNlKGosMSk7CiAgICAgICAgaWYoY250ID09IEkpcmV0dXJuOwogICAgfQp9Cg==