spoj#UCV2013D. Distributing V-Energy

Distributing V-Energy

As you probably know there is a new kind of energy called V-energy which is more afordable than electricity, and has some really interesting properties. The Universal Company on V-Energy has just reached your city and is currently planning the location of the distribution centers. You are given a map of the city, the list of the location of the distribution centers, and they need you to report which is the minimum ammount of energy that would reach a building of the city, and how many buildings share that ammount of energy

V-Energy has the following properties:

 

  •  If a buiding has K units of V-Energy it will consume C units and distribute K - C to every building it is connected to. If K < C the building will consume K units and will not distribute any units of energy
  •  If a building receives V-Energy from di fferent sources it will only consume and distribute the energy with maximum value. For example, if a building receives K = 8 units of energy,and C = 3 it will consume 3 and distribute 5. But, if later the same building receives K = 6 units of energy, it will not consume or distribute this energy since previously it received a larger ammount of energy. If later on the same building receives K = 15 units of energy, it will consume 3 units, and distribute 12 units to its neighbors

As you know, your city is a grid, with buildings on every intersection of the streets. Since V-Energy propagates only through streets, the streets map of the city is perfect for your job. Avenues run horizontally while streets run vertically. Note that sometimes a street or avenue can be blocked. The next figure shows a possible view of a city where street 1 is blocked between
avenues 1 and 2, and avenue 2 is blocked between streets 0 and 1.

 

Streets and avenues sample

Input

The input contains several test cases. Each case starts with a line containing the values K and C (ammount of V-energy each distribution center has and ammount of V-energy each building consumes). (0 <= C,K  <= 10000). The next line contains two values N and M denoting the number of avenues and streets on the city (1 <= N,M <= 1000). The following line will have one value B which denotes the number of street and avenues segments that are blocked and cannot distribute V-energy (0 <= B<=  N*M-N-M). The following B lines will have four values T I J1 J2. T indicates the type of the segment, can be either 'A' of 'S' to denote an avenue segment or a street segment. I denotes the street or avenue index (0 <=I < N). If it is an avenue segment then J1, J2 are the indexes of the starting street and ending street where the avenue is blocked. If it is a street segment, J1, J2 are the indexes of the starting and ending avenues where the street is blocked (0 <= J1 < J2 <= M). The next line will have the
number D of deposits (0 <=  D <= min(1000,N*M)). The following D lines will have a pair Ai Si indicating that on the intersection of avenue Ai with street Si there will be a distribution center. (0 <= Ai < N, 0 <= Si < M).


The end of input is indicated by a test case with K = C = 0.

Output

For each test cases you have to print a line containing two numbers Q and P indicating the minimum amount of energy that reaches a building on the city, and the number of building with that amount of energy.

Example

Input:
2 1
2 3
0
2
0 0
0 2
12 2
3 3
2
A 2 0 1
S 1 1 2
1
2 0
0 0

Output: 0 1
2 1

</p>