Under construction

Still being built. Things may change

Memento Mori

Time limit2.5sMemory limit64 MB

Summary
Count submatrices with exactly four marked cells whose rows increase and whose columns follow the order given by permutation p, with no smaller qualifying submatrix inside.
Level

Hard9 of 10

Topics
Matrix, Dynamic programming, Divide and conquer
Solved
No attempts yet

Problem

Professor Zhang has an n×mn \times m matrix where every element is 0. Professor Zhang changes kk elements of the matrix into 1s.

Given a permutation p1,p2,p3,p4p_1, p_2, p_3, p_4 of 1,2,3,41, 2, 3, 4, Professor Zhang wants to count the submatrices that satisfy all of the following:

  • The submatrix contains exactly 4 ones.
  • Let the positions of the ones 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 (pi−pj)⋅(ci−cj)>0(p_i - p_j) \cdot (c_i - c_j) > 0 for all 1≤i<j≤41 \le i < j \le 4.
  • The chosen submatrix contains no other submatrix that satisfies the two conditions above.

Input

The first line contains an integer TT, the number of test cases. Each test case starts with a line containing three integers nn, mm, and kk (1≤n,m,k≤20001 \le n, m, k \le 2000), the size of the matrix and the number of ones. The next line contains four integers p1,p2,p3,p4p_1, p_2, p_3, p_4, the permutation. Each of the next kk lines contains two integers rir_i and cic_i (1≤ri≤n1 \le r_i \le n, 1≤ci≤m1 \le c_i \le m), the position of the ii-th one. No two ones share the same position.

There are at most 250 test cases, and the total input size is at most 250 KiB.

Output

For each test case, output a single integer: the number of submatrices that satisfy all the requirements.

Examples1

  1. Example 1

    Input
    1
    5 5 4
    1 2 3 4
    1 1
    2 2
    3 3
    4 4
    
    Expected output
    1