//這裡有新的輸入輸出格式,注意其實輸入格式跟輸出格式是分開的,即系就算某一輸出改變在另一輸入前,
//只要輸出格式符合要求就行
//用三重for做
#include<iostream>
#include<string>
using namespace std;
int st[3][30],sttemp[3][30];
char s[10000];
void insert(int n)
{
int i,j;
char s;
//先輸入轉子
for(i=0;i<3;i++)
for(j=0;j<n;j++)
{
cin>>s;
sttemp[i][j]=s-'A';
}
//把編碼轉子轉為解碼轉子
for(i=0;i<3;i++)
for(j=0;j<n;j++)
st[i][sttemp[2-i][j]]=j; //其實就是數組值與下標的對應轉換
for(i=0;i<3;i++)
for(j=0;j<n;j++)
sttemp[i][j]=st[i][j];
}
//此函數是讓第a個轉子移一下位,同時解碼轉子改變
//轉子移一位其實是st[a][i]的值等於其前一個st[a][i+1]的值+1
void rotor(int a,int n)
{
int temp,i;
//由於有覆蓋,要先儲存st[a][n-1]
temp=st[a][n-1];
for(i=n-1;i>=1;i--)
st[a][i]=( st[a][i-1]+1 )%n;
st[a][0]=(temp+1)%n;
}
int main()
{
int n,count=1;
//最外這個迴圈是test迴圈
cin>>n;
while(n!=0)
{
cout<<"Enigma "<<count<<":"<<endl;
count++;
int crypnum;
insert(n);
cin>>crypnum;
//此迴圈是對同一test的不同資料
while(crypnum--)
{
int i,j,k,temp,len;
char t;
bool flags;
cin>>s;
len=strlen(s);
//利用三重for迴圈來方便對轉子何時移位做判斷
//flags記錄當s[]每一位字元碼都譯好以後,退出迴圈
flags=1;
for(i=0;i<len;i++)
{
if(flags==0)
break;
for(j=0;j<n;j++)
{
if(flags==0)
break;
for(k=0;k<n;k++)
{
if(s[i*n*n+j*n+k]=='\0')
{
flags=0;
break;
}
temp=s[i*n*n+j*n+k]-'A';
//輸出st的經過幾個轉子以後的原碼
//字元要藉助t才能輸出
t=st[2][ st[1][ st[0][temp] ] ]+'a';
cout<<t;
rotor(2,n); //轉子2移一位
}
rotor(1,n); //轉子1移一位
}
rotor(0,n); //轉子0移一位
}
cout<<endl;
//轉子複原
for(i=0;i<3;i++)
for(j=0;j<n;j++)
st[i][j]=sttemp[i][j];
}
cin>>n; //為了輸出時能在test之間空行
if(n!=0)
cout<<endl;
}
return 0;
}