2420 - DS_2021_quiz2_stack Scoreboard

Time

2021/11/03 18:30:00 2021/11/03 20:00:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
13336 DS_2021_quiz2_Stack

13336 - DS_2021_quiz2_Stack   

Description

Given n matrixes, the matrix expression is like (A(BC)). We wanted to calculate how many multiplications would take to get the final matrix of this matrix expression.

For instance, A was 50*10 matrix, B was 10*20, C was 20*5, then the number of (A(BC))’s multiplications would be 3500.

    (BC) : 10*20*5 = 1000, (BC) size becomes 10*5

    A*(BC) : 50*10*5 = 2500, A*(BC) size become 50*5

Total multiplication numbers would be 1000+2500 = 3500

 

On the other hand, the expression (AC) would not be allowed since their size did not match(m*n, n*w, n should be the same), so please output error on such test case.

Input

The first line contains an integer t, which is the number of the matrix.

The next n lines, c n m, represented matrix size(n,m) name after c, which c is a single capital letter.

The n+2 line contains an integer q, which is the number of expressions.

The next q lines follow by the matrix expression

Input ranges:

1 <= t,q <= 2000

1 <= n,m <= 100,000

 

Output

Print line: 


If the expression is valid, output the number of multiplications.

If not, output error.

Sample Input  Download

Sample Output  Download

Tags




Discuss