L. Trace of Product of Sparse Square Matrices
time limit per test
1.5 seconds
memory limit per test
960 megabytes
input
standard input
output
standard output
Example
Input
3
4
2 1 6
2 3 6
3 1 4
3 2 3
4
1 1 2
1 3 3
2 3 3
3 2 1
Output
27