Facebook Hacker Cup 2014 Finals
Facebook Headquarters - a mysterious, intimidating place, full of magical code and trade secrets. If outsiders were ever to breach the walls of the compound, which are protected by a legion of security guards and, more importantly, security foxes, the entire company could well be brought to its knees!
Actually, wait, tours of the place are given regularly…
The compound consists of buildings, with walkways running amongst them. The -th walkway connects buildings and , and no two buildings are directly connected by more than one walkway. There are no other ways to move from building to building.
Over a period of days, some events will occur
at the Headquarters daily. One of two types of events will happen on the
-th day, indicated by the value of the character . If T
then a tour will take place — otherwise, S
, and a
security sweep of a building will take place.
If a tour is given on the -th day, visitors will enter the compound at building , and leave from building . If it turns out that these two buildings are not actually connected by any sequence of walkways, then the tour will be cancelled, and the unfortunate visitors will be given Facebook T-shirts and promptly kicked out. Otherwise, a large number of groups of people will be led from building to building along various routes. No route will involve travelling along the same walkway multiple times (even in different directions), but a route might revisit the same building repeatedly, including buildings and . Along the way, some visitors will inevitably get "lost", and fail to rejoin their tour group. How many of them will get away with this will depend on the security levels that day, but it's safe to say that, in total, new outsiders will be left behind in each building which could possibly be part of any valid tour route from building to building . Good thing they'll no doubt have brought cameras to amuse themselves with while they wait to be found.
On the other hand, if a security sweep is conducted on the -th day, then the security guards (and foxes) will carefully search building for any trespassers remaining from previous tours, who will be kindly escorted out of the Headquarters (after being given Facebook T-shirts for the road, of course).
Because this company likes to collect data about everything, you've been hired to record how many outsiders were found in each sweep. However, to make things more interesting, you'll instead simply write a program to predict these values based on the map of the compound and the schedule of events!
Input Specification
Line : integer, , the number of test cases.
For each test case:
Line : integers, , , and .
Next lines: integers, and , for .
Next lines: character, , followed by the three integers
, , and if T
, or the single integer
if S
, for .
Output Specification
For each test case, output integer, the total number of people escorted out of the compound, modulo .
Sample Input
1
6 5 9
1 2
2 3
3 4
4 5
5 3
T 1 2 5
T 5 6 1000
S 2
S 6
T 2 3 1
T 5 3 14
S 1
S 2
S 4
Sample Output
26
Explanation of Sample
On the first day, a tour is given from building to building . The only valid route consists of simply crossing the walkway between these two buildings. As such, by the end of the day, outsiders are left hiding in each of buildings and .
On the second day, the planned low-security tour unfortunately cannot take place, due to no routes existing between buildings and . Facebook should probably hire some new tour planners, as well as architects.
On the following two days, security sweeps of buildings and are carried out, with and outsiders found and removed, respectively.
On the fifth day, a tour is given from building to building . There are exactly three valid routes, passing through buildings , , and . As such, one new outsider remains behind in each of buildings , , , and .
On the sixth day, a tour is given from building to building . There are exactly two valid routes, passing through buildings and . As such, new outsiders take up residence in each of buildings , , and .
Finally, security sweeps of buildings , , and are conducted, evicting , , and people, respectively. In total, then, people will have been escorted out of the compound, which is still when taken modulo . Following these events, buildings and still contain outsiders, while the others contain none.
Comments