text = list(input()) n = int(input()) matrix = [] for i in range(n): matrix.append([el for el in input()]) m = int(input()) for row in range(n): for col in range(n): if matrix[row][col] == "P": start_row = row start_col = col matrix[start_row][start_col] = "-" for i in range(m): command = input() if command == "up": new_row = start_row - 1 new_col = start_col if new_row not in range(n) or new_col not in range(n): text.pop() new_row = start_row new_col = start_col else: next_point = matrix[new_row][new_col] if next_point != "-": text.append(next_point) matrix[new_row][new_col] = "-" elif command == "down": new_row = start_row + 1 new_col = start_col if new_row not in range(n) or new_col not in range(n): text.pop() new_row = start_row new_col = start_col else: next_point = matrix[new_row][new_col] if next_point != "-": text.append(next_point) matrix[new_row][new_col] = "-" elif command == "left": new_row = start_row new_col = start_col - 1 if new_row not in range(n) or new_col not in range(n): text.pop() new_row = start_row new_col = start_col else: next_point = matrix[new_row][new_col] if next_point != "-": text.append(next_point) matrix[new_row][new_col] = "-" elif command == "right": new_row = start_row new_col = start_col + 1 if new_row not in range(n) or new_col not in range(n): text.pop() new_row = start_row new_col = start_col else: next_point = matrix[new_row][new_col] if next_point != "-": text.append(next_point) matrix[new_row][new_col] = "-" start_row = new_row start_col = new_col matrix[start_row][start_col] = "P" print("".join(text)) for row in matrix: print("".join(row))