#include<stdio.h>
int main()
{
int x, y, kase = 0, first = 1;
char s[10][10], c;
while (1) {
c = getchar();
if (c == 'Z')
break;
int j = 0;
do {
if (c == ' ') {
x = 0;
y = j;
}
s[0][j] = c;
j++;
c = getchar();
} while (c != '\n');
if (j == 4) {
s[0][j] = ' ';
x = 0;
y = j;
j++;
}
s[0][j] = '\0';
for (int i = 1; i < 5; ++i) {
j = 0;
while ((c = getchar()) != '\n') {
if (c == ' ') {
x = i;
y = j;
}
s[i][j] = c;
j++;
}
if (j == 4) {
s[i][j] = ' ';
x = i;
y = j;
j++;
}
s[i][j] = '\0';
}
int flag = 1;
// printf("x = %d, y = %d\n", x, y);
while ((c = getchar()) != '0') {
if (!flag || c == '\n')
continue;
if (c == 'A') {
if (x - 1 >= 0) {
s[x][y] = s[x - 1][y];
s[x - 1][y] = ' ';
x = x - 1;
} else {
flag = 0;
}
} else if (c == 'B') {
if (x + 1 < 5) {
s[x][y] = s[x + 1][y];
s[x + 1][y] = ' ';
x = x + 1;
} else {
flag = 0;
}
} else if (c == 'L') {
if (y - 1 >= 0) {
s[x][y] = s[x][y - 1];
s[x][y - 1] = ' ';
y = y - 1;
} else {
flag = 0;
}
} else if (c == 'R') {
if (y + 1 < 5) {
s[x][y] = s[x][y + 1];
s[x][y + 1] = ' ';
y = y + 1;
} else {
flag = 0;
}
}
}
getchar();
if (first)
first = 0;
else
printf("\n");
printf("Puzzle #%d:\n", ++kase);
if (flag) {
for (int i = 0; i < 5; ++i) {
for (j = 0; j < 4; ++j) {
printf("%c ", s[i][j]);
}
printf("%c\n", s[i][4]);
}
} else {
printf("This puzzle has no final configuration.\n");
}
}
return 0;
}