#C7028. Final Plant Positions

    ID: 50854 Type: Default 1000ms 256MiB

Final Plant Positions

Final Plant Positions

You are given a grid of size \( n \times m \) and a sequence of operations represented as a string containing the characters 'U', 'D', 'L', and 'R'. Each character instructs you to move up, down, left, or right respectively. The grid is cyclic, meaning that moving off one edge of the grid wraps around to the opposite edge.

For an initial position \( (x, y) \), the final position after all operations is computed as follows:

[ x_{final} = ((x - 1 + \Delta_v) \bmod n) + 1, \quad y_{final} = ((y - 1 + \Delta_h) \bmod m) + 1 ]

Here, \( \Delta_v \) and \( \Delta_h \) are the net vertical and horizontal moves, respectively. For example, an 'U' decreases \( x \) by 1, while a 'D' increases it by 1; similarly, 'L' decreases \( y \) by 1 and 'R' increases it by 1. Your task is to determine the final positions of several plants after applying all the operations to the grid.

inputFormat

The input is provided via standard input and has the following format:

  1. The first line contains two integers \( n \) and \( m \) – the number of rows and columns of the grid.
  2. The second line contains a string of characters ('U', 'D', 'L', 'R') describing the sequence of operations.
  3. The third line contains an integer \( q \) representing the number of queries.
  4. Each of the next \( q \) lines contains two integers \( x \) and \( y \), which are the initial coordinates of a plant.

outputFormat

For each query, output a single line containing two space-separated integers representing the final coordinates of the plant after all operations are applied.

## sample
5 5
UUDDLLRR
3
1 1
2 2
5 5
1 1

2 2 5 5

</p>