For the scope of this problem, an indeterministic expression refers to an expression with no fixed value, but with uncertainties. Such uncertainties are caused by variables (i.e., symbols) with unknown values.
Examples of deterministic expression:
2*3*5*(7+11)
2022+5-14
998244353
Examples of indeterministic expression:
a+b
x*233+42
(x+y)*(z-1)
q+q+q+q+q+q
Formally, the grammar describing a valid indeterministic expression is as below:
E ::= E '+' T | E '-' T | T
T ::= T '*' F | F
F ::= '(' E ')' | S | N
Here, S stands for symbol, which is a single lower-case Latin letter (a-z). N stands for numeric literal, which is one or several digits,
without leading sign (+ or -) or leading zero.
Operations have standard priority. Multiply has the higher priority than plus and minus. Operations are left associative. Please also note that divide (/) is not supported for the scope of this problem.
输入描述:
The input starts with an integer
(
), the number of test cases.
For each test case, the first line contains an integer
(
), denoting the number of random variables.
In the next
lines, the
-th line starts with a single lower-case Latin letter
, and an integer
(
), which is the number of distinct possible values followed.
are all different. Then
pairs of integers and real numbers
(
,
). Each of
must have 6 digits after their decimal points, and
. These pairs define a probability mass function:
. In other words, the variable
has probability
to take the value
(
).
The final line of each test case is a valid indeterministic expression, consisting of lower-case letters that has appeared in the previous
lines, digits, signs including +, -, *, (, ) . There is no extra space or other invisible characters. Each variable listed previously must appear in the expression at least once. The numeric literals appearing in the expression are all non-negative integers and never exceed
.
We guarantee that the sum of expression lengths from all test cases does not exceed
.
输出描述:
For each test case, output the expected value of the expression modulo
. It can be shown that the answer can be represented as
such that
and
are coprime integers and
. Print the value of
.
示例1
输入
复制
2
2
a 2 1 0.500000 2 0.500000
b 2 1 0.300000 2 0.700000
a+b
3
x 1 42 1.000000
y 3 4 0.333333 6 0.333333 8 0.333334
z 2 233 0.570000 250 0.430000
x*1+(x+y)*(z-z)
说明
For all the different symbols appearing at least once in the expression, we let them be random variables with known distributions. Specifically, for every variable, we have its probability mass function, which gives out the probability that the discrete random variable is exactly equal to some value. The denotion of
is the probability that random variable
has value
.
Given the distributions of all variables, we ask: what is the expected value (i.e., expectation) of the whole expression. Recall that the expected value of an expression with a finite number of outcomes is an average of all possible outcomes, weighted by the probabilities of the outcomes.
For sample test case 1: The answer

because

. We then explain why the answer is

.
- The expression equals to 2 with probability 0.15.
. - The expression equals to 3 with probability 0.5.
. - The expression equals to 4 with probability 0.35.
.
The answer is therefore

.