#include "iostream"
#include "map"
#include "string"
using namespace std;
int main()
{
int TestCase;
cin >> TestCase;
cin.get();
int Case = 0;
while (TestCase--)
{
Case++;
string str1, str2;
getline(cin, str1);
getline(cin, str2);
int size1 = str1.size();
int size2 = str2.size();
map<char, char> m;
map<char, char>::iterator it;
char alphabet[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
for (int i = 0; i < size2; i++)
m.insert(pair<char, char>(alphabet[i], str2[i]));
string temp = "";
for (int i = 0; i < size1; i++)
{
if (str1[i] != ' ')
temp += m[str1[i]];
else
temp += ' ';
}
cout << Case << " " << temp << endl;
}
}