|
Looking for a new direction for his company, Carlos wants to break into the |
|
most exciting tech field: digital rights management (DRM). In the world of |
|
DRM, mega-corporations wage perpetual war against a vast sea of e-pirates, |
|
modern-day Robin Hoods of ambiguous moral character. |
|
|
|
One of the biggest players in this struggle is Sony, the well-known |
|
copyrighter of numbers. Considering the speed with which Sony puts out new |
|
products, it's no wonder they're having trouble coming up with enough secret |
|
encryption keys to protect all of their intellectual property. Enter Carlos. |
|
|
|
_ "Gone are the days of paying over-priced number theory PhDs to craft primes |
|
by hand. I can make you a system that will generate all the numbers you need, |
|
to your exact specifications." _ |
|
|
|
Sony wants to see a demonstration of Carlos's system before forking over |
|
millions of dollars in consultancy fees. They have some new products in |
|
development, each of which requires a secret key, **X**. For each key, Sony |
|
has a list of **N** requirements. The _i_th requirement has an operator |
|
character **Oi**, an integer value **Vi**, and an integer result **Ri**. |
|
|
|
When **Oi** is 'G', the _i_th requirement states that the greatest common |
|
divisor of **X** and **Vi** must be **Ri**. That is, `GCD(X, Vi) = Ri`. |
|
|
|
When **Oi** is 'L', the _i_th requirement states that the least common |
|
multiple of **X** and **Vi** must be **Ri**. That is, `LCM(X, Vi) = Ri`. |
|
|
|
There is also a global requirement that all of Sony's secret keys must be |
|
positive integers no larger than 1,000,000,000. Help Carlos build any positive |
|
integer **X** consistent with all of these requirements, or determine that no |
|
such integer exists. |
|
|
|
### Input |
|
|
|
Input begins with an integer **T**, the number of secret keys that Sony wants |
|
Carlos to generate. For each number, there is first a line containing the |
|
integer **N**. Then, **N** lines follow, the _i_th of which contains the |
|
character **Oi**, and the integers **Vi** and **Ri**, all separated by spaces. |
|
|
|
### Output |
|
|
|
For the _i_th secret key, print a line containing "Case #_i_: " followed by a |
|
single integer: your chosen value of **X**, or -1 if no valid integer **X** |
|
exists. |
|
|
|
### Constraints |
|
|
|
1 ≤ **T** ≤ 250 |
|
1 ≤ **N** ≤ 2,000 |
|
**Oi** ∈ {'G', 'L'} |
|
1 ≤ **Vi**, **Ri** ≤ 1,000,000,000 |
|
|
|
### Explanation of Sample |
|
|
|
In the first case, `GCD(6, 4) = 2`, meaning that **X** = 6 satisfies the one |
|
and only requirement. Note that, for this case and potentially other cases |
|
below, **various other outputs will also be accepted**. |
|
|
|
In the second case, there exists no valid integer **X** such that 1 ≤ **X** ≤ |
|
1,000,000,000 and `LCM(X, 4) = 2`. |
|
|
|
In the third case, `GCD(24, 18) = 6`, `LCM(24, 40) = 120`, and `GCD(24, 20) = |
|
4`, meaning that **X** = 24 is a valid choice. |
|
|
|
|