#P11017. [2016杭电多校]Memento Mori

[2016杭电多校]Memento Mori

Memento Mori

Problem Description

Professor Zhang has an n×mn \times m zero matrix(i.e. a matrix consisting of all 0s). Professor Zhang changes kk elements in the matrix into 1. Given a permutation pp of {1,2,3,4}\{1,2,3,4\}, Professor Zhang wants to find the number of such submatrices that:

  1. the number of 1s in the submatrix is exactly 4.
  2. let the positions of the 1s in the submatrix be (r1,c1),(r2,c2),(r3,c3),(r4,c4)(r_1,c_1),(r_2,c_2),(r_3,c_3),(r_4,c_4), then r1<r2<r3<r4r_1 < r_2 < r_3 < r_4 and (pipj)(cicj)>0(p_i - p_j) \cdot (c_i - c_j) > 0 for all 1i<j41 \le i < j \le 4.
  3. no other submatrices inside the submatrix meet the above two conditions.

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case: The first line contains three integers nn, mm and kk (1n,m,k2000)(1 \le n,m,k \le 2000)-- the size of the matrix and the number of 1s. The second line contains four integers p1,p2,p3,p4p_1,p_2,p_3,p_4 denoting the permutation. Each of the next kk lines contains two integers rir_i and cic_i (1rin,1cim)(1 \le r_i \le n, 1 \le c_i \le m) -- the position of the ii-th 1. No two 1s will be in the same position.

Output

For each test case, output an integer denoting answer.

Sample Input

1
5 5 4
1 2 3 4
1 1
2 2
3 3
4 4

Sample Output

1

Author

zimpha

Source

2016 Multi-University Training Contest 2