#B. 矩阵乘法

    Type: Default 1000ms 256MiB

矩阵乘法

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

Background

这确实只是一道简单的矩阵乘法……

Description

稀疏矩阵乘法在实际应用中十分重要,在本题中你将被要求实现关于它的一个简单算法。

给定一个n×mn\times m的整系数稀疏矩阵AMn×m(Z)A\in M_{n\times m}(\mathbb{Z}),以及一个m×nm\times n的稀疏矩阵BMm×n(Z)B\in M_{m\times n}(\mathbb{Z}),我们可以得到它们的乘积C=ABMn×n(Z)C=AB\in M_{n\times n}(\mathbb{Z})

为了简单起见,稀疏矩阵利用COO形式(坐标形式)给出:对于矩阵AA,我们给定一个pp,表示矩阵AA只有pp个元素非零,然后我们将给出这pp个非零元的具体信息;对于矩阵BBqq个非零元也类似地给出。

由于CC的尺寸可能很大,所以在本题中我们只关心它的迹,即tr C=i=1nCii\text{tr }C=\sum\limits_{i=1}^nC_{ii}

Format

Input

第一行依次给出四个正整数n,m,p,qn,m,p,q,其中n,mn,m为矩阵的尺寸,其意义如题意所示。

接下来pp行,每行三个整数xi,yi,zix_i,y_i,z_i,每行表示AA矩阵的一个非零元Axi,yi=ziA_{x_i,y_i}=z_i,数据保证1xin,1yim1\le x_i\le n,1\le y_i\le m,并且点对(xi,yi)(x_i,y_i)两两不同。

接下来qq行,每行三个整数xi,yi,zix_i,y_i,z_i,每行表示BB矩阵的一个非零元Bxi,yi=ziB_{x_i,y_i}=z_i,数据保证1xim,1yin1\le x_i\le m,1\le y_i\le n,并且点对(xi,yi)(x_i,y_i)两两不同。

Output

输出一行一个整数,表示乘积矩阵CC的迹,即tr AB\text{tr }AB的值。

Samples

5 5 3 4
1 2 3
4 5 6
5 3 -6
2 4 9
5 3 8
3 5 -2
2 1 4
24

Limitation

1n,m,p,q1051\le n,m,p,q\le 10^5

zi106|z_i|\le 10^6

Explanation

A=(0300000000000000000600600)A=\begin{pmatrix} 0 & 3 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 6\\ 0 & 0 & -6 & 0 & 0 \end{pmatrix}

B=(0000040090000020000000800)B=\begin{pmatrix} 0 & 0 & 0 & 0 & 0\\ 4 & 0 & 0 & 9 & 0\\ 0 & 0 & 0 & 0 & -2\\ 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 8 & 0 & 0 \end{pmatrix}

C=(12002700000000000004800000012)C=\begin{pmatrix} 12 & 0 & 0 & 27 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 48 & 0 & 0 \\ 0 & 0 & 0 & 0 & 12 \end{pmatrix}