#include <iostream>
#include <cstring>
using namespace std;
int f;
char order[10000];
char orderplus[10000];
string map[5];
void swap(char &a,char &b)
{
char c;
c=a;
a=b;
b=c;
}
bool exc(int a,int b)
{
if(a<0||b<0||a>=5||b>=5)
{
f=1;
return true;
}
else
return false;
}
int main()
{
int num=0;
while(1)
{
memset(order,0,sizeof(order));
f=0;
int x,y;
for(int i=0; i<5; i++)
{
getline(cin,map[i]);
if(map[0][0]=='Z')
return 0;
}
if(num>0)
cout<<endl;
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
if(map[i][j]==' ')
{
x=i;
y=j;
}
int p=0;
char now;
while((now=getchar())!='0')
order[p++]=now;
cin.get();
for(int i=0; i<p; i++)
{
if(order[i]=='A')
{
if(exc(x-1,y))
break;
swap(map[x-1][y],map[x][y]);
x=x-1;
y=y;
}
else if(order[i]=='R')
{
if(exc(x,y+1))
break;
swap(map[x][y+1],map[x][y]);
x=x;
y=y+1;
}
else if(order[i]=='B')
{
if(exc(x+1,y))
break;
swap(map[x+1][y],map[x][y]);
x=x+1;
y=y;
}
else if(order[i]=='L')
{
if(exc(x,y-1))
break;
swap(map[x][y-1],map[x][y]);
x=x;
y=y-1;
}
}
printf("Puzzle #%d:\n",++num);
if(f)
cout<<"This puzzle has no final configuration.\n";
else
{
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<map[i][j];
if(j<4)
cout<<" ";
}
cout<<endl;
}
}
}
}