// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#define dibs reserve
#define OVER9000 1234567890
#define tisic 47
#define soclose 10e-7
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define uint unsigned int
// mylittlepony
using namespace std;
int GCD(int a, int b) {
if(a > b) swap(a,b);
if(a == 0) return b;
return GCD(b%a,a);}
int main() {
int T,N;
cin >> T;
for(int t =0; t < T; t++) {
string s,code;
vector<int> dec(26,-1);
vector<int> codeI(26);
cin >> N >> s >> code;
for(int i =0; i < 26; i++) codeI[code[i]-'a'] =i;
vector< pair<int,int> > C;
for(int i =0; i < s.length(); i++) if(dec[s[i]-'a'] < 0) {
int akt =s[i]-'a', c =0;
vector< pair<int,int> > V;
vector<int> V0;
while(dec[akt] == -1) {
dec[akt] =-2;
V.push_back(make_pair(akt,c));
V0.push_back(akt);
c++;
akt =codeI[akt];}
sort(V.begin(),V.end());
// choose a valid solution
int x =V.size()-1;
for(int j =0; j < V.size(); j++) {
bool ok =true;
for(int k =0; k < C.size(); k++) {
int d =GCD(C[k].ff,GCD(c,C[k].ss));
if(V[j].ss%d != 0) ok =false;
d =GCD(C[k].ff,GCD(c,V[j].ss));
if(C[k].ss%d != 0) ok =false;
if((C[k].ss-V[j].ss)%GCD(c,C[k].ff) != 0) ok =false;}
if(ok) {
x =V[j].ss;
C.push_back(make_pair(c,V[j].ss));
break;}
}
for(int j =0; j < V0.size(); j++) dec[V0[j]] =V0[(j+x)%V0.size()];
}
for(int i =0; i < s.length(); i++) cout << (char)('a'+dec[s[i]-'a']);
cout << "\n";}
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cm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MFtqXV0gPVYwWyhqK3gpJVYwLnNpemUoKV07CiAgICAJCX0KICAgIAlmb3IoaW50IGkgPTA7IGkgPCBzLmxlbmd0aCgpOyBpKyspIGNvdXQgPDwgKGNoYXIpKCdhJytkZWNbc1tpXS0nYSddKTsKICAgIAljb3V0IDw8ICJcbiI7fQogICAgcmV0dXJuIDA7fQogICAgICAgIAovLyBsb29rIGF0IG15IGNvZGUKLy8gbXkgY29kZSBpcyBhbWF6aW5n
Mwo3CnB1bWl1YXUKeG11cnB3ZWxpZnNvYmRocWtjYWp0em5ndnkKMjkKeXpxanh5bnp5c3lybXR0YmpheW15cmd5aXN2bm8KemNkZWZnaGlqa2Ftbm9wcXJzdHVsd3h5YnYKMjQKYmV6bW5mdGhldWVjdGhlcGVndWNhZ3R6CmNiYWZlZGloZ2xram9ubXJxcHV0c3h3dnp5
3
7
pumiuau
xmurpwelifsobdhqkcajtzngvy
29
yzqjxynzysyrmttbjaymyrgyisvno
zcdefghijkamnopqrstulwxybv
24
bezmnftheuecthepegucagtz
cbafedihglkjonmrqputsxwvzy