beecrowd | 1356
# Jupiter Attacks!

**Timelimit: 3**

By Fidel I. Schaposnik Argentinaa

Jupiter is invading! Major cities have been destroyed by Jovian spacecrafts and humanity is fighting back. Nlogonia is spearheading the counter-offensive, by hacking into the spacecrafts’ control system.

Unlike Earthling computers, in which usually a byte has 2^{8} possible values, Jovian computers use bytes with B possible values, {0,1,...,B−1}. Nlogonian software engineers have reverse-engineered the firmware for the Jovian spacecrafts, and plan to sabotage it so that the ships eventually selfdestruct.

As a security measure, however, the Jovian spacecrafts run a supervisory program that periodically checks the integrity of the firmware, by hashing portions of it and comparing the result against known good values. To hash the portion of the firmware from the byte at position i to the byte at position j, the supervisor uses the hash function

where P is a prime number. For instance, if B = 20 and P = 139, while bytes 2 to 5 of the firmware have the values *f*_{2} = 14, *f*_{3} = 2, *f*_{4} = 2 and *f*_{5} = 4 then

H(*f*_{2}, . . . *f*_{5}) = B^{0 }*f*_{5} + B^{1 }*f*_{4} + B^{2} *f*_{3} + B^{3 }*f*_{2} (mod P)

= 20^{0} × 4 + 20^{1} × 2 + 20^{2} × 2 + 20^{3} × 14 (mod 139)

= 4 + 40 + 800 + 112000 (mod 139)

= 112844 (mod 139)

= 115

The Nlogonian cryptologists need to find a way to sabotage the firmware without tripping the supervisor. As a first step, you have been assigned to write a program to simulate the interleaving of two types of commands: editing bytes of the firmware by the Nlogonian software engineers, and computing hashes of portions of the firmware by the Jovian supervisory program. At the beginning of the simulation the value of every byte in the firmware is zero.

Each test case is described using several lines. The first line contains four integers **B, P, L** and **N**, where **B** is the number of possible values of a Jovian byte,** P** is the modulus of the Jovian hash (2 ≤ **B** < **P** ≤ 10^{9} and **P** prime),** L** is the length (number of Jovian bytes) of the spacecrafts’ firmware, and **N** is the number of commands to simulate (1 ≤** L, N** ≤ 10^{5}). At the beginning of the simulation the value of every byte in the firmware is f_{i} = 0 for 1 ≤ **i** ≤ **L**. Each of the next **N** lines describes a command to simulate. Each command description starts with an uppercase letter that is either ‘E’ or ‘H’, with the following meanings.

- 'E': The line describes an edit command. The letter is followed by two integers
**I**and**V**indicating that the byte at position**I**of the firmware (that is, f_{I}) must receive the value**V**(1 ≤**I**≤**L**and 0 ≤**V**≤**B**− 1). - 'H': The line describes a hash command. The letter is followed by two integers
**I**and**J**indicating that H(f_{I}, . . . f_{J}) must be computed (1 ≤**I**≤**J**≤**L**).

The last test case is followed by a line containing four zeros.

For each test case output the results of the hash commands in the input. In the i-th line write an integer representing the result of the i-th hash command. Print a line containing a single character '-' (hyphen) after each test case.

Sample Input | Sample Output |

20 139 5 7 |
115 |