cho.sh
Notes
Loading...

King

Time limit

2s

Memory limit

128 MB

Problem

An 8×8 chessboard contains one king and one stone. A square is written as one letter followed by one number. The letter gives the column, from A on the far left to H on the far right. The number gives the row, from 1 at the bottom to 8 at the top. For example, A1 is the lower-left corner, and B1 is the square immediately to its right.

The king can move in the following ways.

  • R: one square to the right
  • L: one square to the left
  • B: one square down
  • T: one square up
  • RT: one square diagonally up-right
  • LT: one square diagonally up-left
  • RB: one square diagonally down-right
  • LB: one square diagonally down-left

If the king moves onto the square containing the stone, the stone is pushed one square in the same direction as the king's move.

Process the given move commands in order. If a command would move either the king or the stone outside the chessboard, ignore that command and continue with the next one.

Write a program that prints the final positions of the king and the stone.

Input

The first line contains the king's position, the stone's position, and the number of moves N. Each of the next N lines contains one move command for the king. N is a natural number no greater than 50, and every command is one of the eight commands listed above.

Output

Print the king's final position on the first line and the stone's final position on the second line.