#include<iostream>
#include<queue>
#include<algorithm>
#include<list>
#include<map>
#include<vector>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define Author "DemoVersion"
#define MAXN -
#define ull unsigned long long
const int inf (3<<30);
char chmap[255];
bool isadd[255];
void en(string &str){
for(int i=0;i<str.length();i++)
str[i]=chmap[str[i]];
}
int main()
{
map<string,bool> m;
int i,T,N;
string text,key,tmp,ftmp,finals;
cin>>T;
while(T--){
m.clear();
cin>>N>>text>>key;
for(i=0;i<255;i++)isadd[i]=false;
for(i=0;i<26;i++)chmap['a'+i]=key[i];
tmp="";
for(i=0;i<text.length();i++){
if(isadd[text[i]]==0){
isadd[text[i]]=1;
tmp+=text[i];
}
}
ftmp=tmp;
m[tmp]=true;
while(1){
en(tmp);
if(m.count(tmp)>0)break;
m[tmp]=true;
}
finals=m.begin()->first;
for(int i=0;i<ftmp.length();i++)
chmap[ftmp[i]]=finals[i];
for(i=0;i<text.length();i++)
text[i]=chmap[text[i]];
cout<<text<<endl;
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHF1ZXVlPgojaW5jbHVkZTxhbGdvcml0aG0+CiNpbmNsdWRlPGxpc3Q+CiNpbmNsdWRlPG1hcD4KI2luY2x1ZGU8dmVjdG9yPgojaW5jbHVkZTxzdHJpbmc+CiNpbmNsdWRlPGNzdHJpbmc+CiNpbmNsdWRlPGNzdGRsaWI+CiNpbmNsdWRlPGNzdGRpbz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBBdXRob3IgIkRlbW9WZXJzaW9uIgojZGVmaW5lIE1BWE4gLQojZGVmaW5lIHVsbCB1bnNpZ25lZCBsb25nIGxvbmcKY29uc3QgaW50IGluZiAoMzw8MzApOwpjaGFyIGNobWFwWzI1NV07CmJvb2wgaXNhZGRbMjU1XTsKdm9pZCBlbihzdHJpbmcgJnN0cil7CiAgICBmb3IoaW50IGk9MDtpPHN0ci5sZW5ndGgoKTtpKyspCiAgICAgICAgc3RyW2ldPWNobWFwW3N0cltpXV07Cn0KaW50IG1haW4oKQp7CiAgICBtYXA8c3RyaW5nLGJvb2w+IG07CiAgICBpbnQgaSxULE47CgogICAgc3RyaW5nIHRleHQsa2V5LHRtcCxmdG1wLGZpbmFsczsKICAgIGNpbj4+VDsKICAgIHdoaWxlKFQtLSl7CiAgICAgICAgbS5jbGVhcigpOwogICAgICAgIGNpbj4+Tj4+dGV4dD4+a2V5OwogICAgICAgIGZvcihpPTA7aTwyNTU7aSsrKWlzYWRkW2ldPWZhbHNlOwogICAgICAgIGZvcihpPTA7aTwyNjtpKyspY2htYXBbJ2EnK2ldPWtleVtpXTsKICAgICAgICB0bXA9IiI7CiAgICAgICAgZm9yKGk9MDtpPHRleHQubGVuZ3RoKCk7aSsrKXsKICAgICAgICAgICAgaWYoaXNhZGRbdGV4dFtpXV09PTApewogICAgICAgICAgICAgICAgaXNhZGRbdGV4dFtpXV09MTsKICAgICAgICAgICAgICAgIHRtcCs9dGV4dFtpXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmdG1wPXRtcDsKICAgICAgICBtW3RtcF09dHJ1ZTsKICAgICAgICB3aGlsZSgxKXsKICAgICAgICAgICAgZW4odG1wKTsKICAgICAgICAgICAgaWYobS5jb3VudCh0bXApPjApYnJlYWs7CiAgICAgICAgICAgIG1bdG1wXT10cnVlOwogICAgICAgIH0KICAgICAgICBmaW5hbHM9bS5iZWdpbigpLT5maXJzdDsKICAgICAgICBmb3IoaW50IGk9MDtpPGZ0bXAubGVuZ3RoKCk7aSsrKQoJCQljaG1hcFtmdG1wW2ldXT1maW5hbHNbaV07CiAgICAgICAgZm9yKGk9MDtpPHRleHQubGVuZ3RoKCk7aSsrKQoJCQl0ZXh0W2ldPWNobWFwW3RleHRbaV1dOwogICAgICAgIGNvdXQ8PHRleHQ8PGVuZGw7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==
Mwo3CnB1bWl1YXUKeG11cnB3ZWxpZnNvYmRocWtjYWp0em5ndnkKMjkKeXpxanh5bnp5c3lybXR0YmpheW15cmd5aXN2bm8KemNkZWZnaGlqa2Ftbm9wcXJzdHVsd3h5YnYKMjQKYmV6bW5mdGhldWVjdGhlcGVndWNhZ3R6CmNiYWZlZGloZ2xram9ubXJxcHV0c3h3dnp5
3
7
pumiuau
xmurpwelifsobdhqkcajtzngvy
29
yzqjxynzysyrmttbjaymyrgyisvno
zcdefghijkamnopqrstulwxybv
24
bezmnftheuecthepegucagtz
cbafedihglkjonmrqputsxwvzy