#include <iostream>
#include <string>
using namespace std;
int main()
{
int strwide;
cin >> strwide;
pair< char, char > rotor[3][1000];
pair< char, char > rotor_instead[3][1000];
string input;
int t = 0;
while( t < 3 )
{
cin >> input;
if ( input.size() == strwide )
{
char start_1 = 'a';
char start_else = 'A';
if ( t == 0 )
{
for ( int m = 0; m < input.size(); m++ )
{
rotor[t][m].first = start_1;
rotor[t][m].second = input[m];
start_1 = start_1 + 1;
}
}
else
{
for ( int m = 0; m < input.size(); m++ )
{
rotor[t][m].first = start_else;
rotor[t][m].second = input[m];
start_else = start_else + 1;
}
}
//rotor[t] = input;
t++;
}
}
int crypto_number;
cin >> crypto_number;
//string *crypto = new string[crypto_number];
string crypto[1000];
t = 0;
string input_crypto;
while( cin >> input_crypto && input_crypto != "0" && t < crypto_number )
{
crypto[t] = input_crypto;
t++;
}
/*for ( int l = 0; l < t; l++ )
{
cout << crypto[l]<< endl;
}*/
//string *crypto_return = new string[crypto_number];
for ( int i = 0; i < crypto_number; i++ )
{
for( int a = 0; a < 3; a++ )
{
for ( int b = 0; b < strwide; b++ )
{
rotor_instead[a][b] = rotor[a][b];
}
}
//int second_rotor_count = 0;
//char *crypto_return = new char[crypto[i].size()];
char crypto_return[1000];
//cout << crypto[i].size() << endl;
for ( int j = 0; j < crypto[i].size(); j++ )
{
for ( int r = 0; r < strwide; r++ )
{
if ( rotor_instead[2][r].second == crypto[i][j])
{
crypto_return[j] = rotor_instead[2][r].first;
for ( int k = 0; k < strwide; k++ )
{
if ( rotor_instead[1][k].second == crypto_return[j])
{
crypto_return[j] = rotor_instead[1][k].first;
for ( int s = 0; s < strwide; s++ )
{
if ( rotor_instead[0][s].second == crypto_return[j] )
{
crypto_return[j] = rotor_instead[0][s].first;
}
}
}
}
}
}
for ( int d = 0; d < strwide; d++ )
{
rotor_instead[0][d].first += 1;
rotor_instead[0][d].second += 1;
if (rotor_instead[0][d].first == 'a'+ strwide )
{
rotor_instead[0][d].first -= strwide;
}
if ( rotor_instead[0][d].second == 'A'+strwide )
{
rotor_instead[0][d].second -= strwide;
}
}
if ( (j+1)%strwide == 0 )
{
//second_rotor_count++;
for ( int d = 0; d < strwide; d++ )
{
rotor_instead[1][d].first += 1;
rotor_instead[1][d].second += 1;
if ( rotor_instead[1][d].first == 'A'+strwide )
{
rotor_instead[1][d].first -= strwide;
}
if ( rotor_instead[1][d].second == 'A'+strwide )
{
rotor_instead[1][d].second -= strwide;
}
}
}
if ( (j + 1)%( strwide*strwide) == 0 )
{
for ( int d = 0; d < strwide; d++ )
{
rotor_instead[2][d].first += 1;
rotor_instead[2][d].second += 1;
if ( rotor_instead[2][d].first == 'A'+strwide )
{
rotor_instead[2][d].first -= strwide;
}
if ( rotor_instead[2][d].second == 'A'+strwide )
{
rotor_instead[2][d].second -= strwide;
}
}
}
}
/*for ( int a = 0; a < 3; a++ )
{
for ( int b = 0; b < strwide; b++ )
{
cout << rotor_instead[a][b].first <<" "<< rotor_instead[a][b].second << endl;
}
}*/
cout << "Enigma "<< i+1 <<":"<< endl;
for ( int c = 0; c < crypto[i].size(); c++ )
{
cout << crypto_return[c];
}
cout << endl;
if ( i != crypto_number - 1 )
{
cout << endl;
}
}
return 0;
}