#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(char a, char b)
{
return(a == b);
}
void DiGraph(vector <string> & digraph, string inpCip)
{
string temp;
for(int k = 0; k < inpCip.length(); k+=2)
{
temp = "";
if(inpCip.length() - k >= 2)
{
temp += inpCip[k];
temp += inpCip[k+1];
}
else
{
temp += inpCip[k];
}
digraph.push_back(temp);
}
}
void search(int row[1], int col[1], string digraph, string box[5])
{
for(int k = 0; k < 5; k++)
for(int j = 0; j < 5; j++)
{
if(digraph[0] == box[k][j])
{
row[0] = k;
col[0] = j;
}
}
for(int k = 0; k < 5; k++)
for(int j = 0; j < 5; j++)
{
if(digraph[1] == box[k][j])
{
row[1] = k;
col[1] = j;
}
}
}
int main()
{
string inpDec,inpCip;
vector <char> puz;
int testC;
string alpha = "abcdefghijklmnoprstuvwxyz";
vector <char> ::iterator mii;
vector <char> ::iterator end;
int row[1], col[1];
vector <string> digraph;
cin >> testC;
getline(cin, inpDec);
for(int i = 0; i < testC; i++)
{
string box[5] = {"", "", "", "", ""};
getline(cin, inpDec);
getline(cin, inpCip);
for(int k = 0; k < inpDec.length() - 1; k++)
for(int j = k + 1; j < inpDec.length(); j++)
{
if(inpDec[k] == inpDec[j])
inpDec[j] = ' ';
}
for(int k = 0; k < inpDec.length(); k++)
if(inpDec[k] != ' ')
puz.push_back(inpDec[k]);
mii = puz.begin();
string tempAlph = alpha;
for(;mii != puz.end(); mii++)
{
for(int k = 0; k < tempAlph.length(); k++)
if(*mii == tempAlph[k])
tempAlph[k] = '0';
}
mii = puz.begin();
int ind = 0;
for(int k = 0; k < 5; k++)
for(int j = 0; j < 5; j++)
{
if(mii != puz.end())
{
box[k] += *mii;
mii++;
}
else
{
here:;
if(tempAlph[ind] != '0')
box[k] += tempAlph[ind];
else
{
ind++;
goto here;
}
ind++;
}
}
for(int k = 0; k < inpCip.length(); k++)
{
if(inpCip[k] == ' ')
{
inpCip.erase(k, 1);
k--;
}
}
string temp;
bool changes = true;
DiGraph(digraph,inpCip);
while(changes == true)
{
changes = false;
temp = "";
// for(int k = 0; k < digraph.size(); k++)
// cout << digraph[k] << " ";
// cout << endl;
for(int k = 0; k < digraph.size(); k++)
if(((digraph[k][0] == digraph[k][1]) || digraph[k].length() == 1)
&& k < digraph.size())
{
temp += digraph[k][0];
temp += 'x';
if(digraph[k].length() == 2)
temp += digraph[k][1];
changes = true;
}
else
temp += digraph[k];
// cout << temp << endl;
if(changes == true)
{
if(temp.length() % 2 == 1 && temp[temp.length()-1] == 'x')
temp.erase(temp.length()-1,1);
digraph.clear();
DiGraph(digraph,temp);
}
//for(int k = 0; k < digraph.size(); k++)
// cout << digraph[k] << " ";
//cout << endl;
}
for(int k = 0; k < digraph.size(); k++)
{
search(row,col, digraph[k], box);
if(digraph[k].size() == 1)
continue;
if(row[0] == row[1])
{
if(col[0] == 4)
cout << char(toupper(box [row[0]] [0]));
else
cout << char(toupper(box [row[0]] [++col[0]]));
if(col[1] == 4)
cout << char(toupper(box [row[1]] [0]));
else
cout << char(toupper(box [row[1]] [++col[1]]));
}
else if(col[0] == col[1])
{
if(row[0] == 4)
cout << char(toupper(box [0][col[0]]));
else
cout << char(toupper(box [++row[0]][col[0]]));
if(row[1] == 4)
cout << char(toupper(box [0][col[1]]));
else
cout << char(toupper(box [++row[1]][col[1]]));
}
else
{
cout << char(toupper(box[row[0]][col[1]]))
<< char(toupper(box[row[1]][col[0]]));
}
}
cout << endl;
puz.clear();
digraph.clear();
}
return 0;
}