#include<iostream>
#include<string>
#define N 5
int main()
{
int posx, posy;
int puzzleNum = 1;
std::string arr[N];
std::string operate;
std::string order;
bool ok;
while (1)
{
std::getline(std::cin, arr[0]);
if (arr[0][0] == 'Z')break;
if (puzzleNum != 1) std::cout << std::endl;
for (int i = 1; i < N; i++)
{
std::getline(std::cin, arr[i]);
}
operate = "";
while (1)
{
std::getline(std::cin, order);
operate += order;
if (order[order.length() - 1] == '0')
break;
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (arr[i][j] == ' ')
{
posy = i + 1;
posx = j + 1;
}
}
}
ok = true;
for (int i = 0; i < operate.length() && ok; i++)
{
if (operate[i] == 0) break;
switch (operate[i])
{
case 'A':
if (posy == 1)
{
ok = false;
break;
}
else
{
arr[posy - 1][posx - 1] = arr[posy - 2][posx - 1];
arr[posy - 2][posx - 1] = ' ';
posy--;
}
break;
case 'B':
if (posy == 5)
{
ok = false;
break;
}
else
{
arr[posy - 1][posx - 1] = arr[posy][posx - 1];
arr[posy][posx - 1] = ' ';
posy++;
}
break;
case 'R':
if (posx == 5)
{
ok = false;
break;
}
else
{
arr[posy - 1][posx - 1] = arr[posy - 1][posx];
arr[posy - 1][posx] = ' ';
posx++;
}
break;
case 'L':
if (posx == 1)
{
{
ok = false;
break;
}
}
else
{
arr[posy - 1][posx - 1] = arr[posy - 1][posx - 2];
arr[posy - 1][posx - 2] = ' ';
posx--;
}
break;
default:
break;
}
}
std::cout << "Puzzle #" << puzzleNum++ << ":" << std::endl;
if (ok)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++) {
if (j)
std::cout << " ";
std::cout << arr[i][j];
}
std::cout << std::endl;
}
}
else
{
std::cout << "This puzzle has no final configuration." << std::endl;
}
}
return 0;
}