
149  The 2017 China Collegiate Programming Contest, Qinhuangdao Site  A
The 2017 China Collegiate Programming Contest Qinhuangdao Site is coming! There will be \(n\) teams participating in the contest, and the contest will be held on a huge round table with \(m\) seats numbered from 1 to \(m\) in clockwise order around it. The \(i\)th team will be seated on the \(s_i\)th seat. BaoBao, an enthusiast for competitive programming, has made \(p\) predictions of the contest result before the contest. Each prediction is in the form of \((a_i,b_i)\), which means the \(a_i\)th team solves a problem during the \(b_i\)th time unit. As we know, when a team solves a problem, a balloon will be rewarded to that team. The participants will be unhappy if the balloons take almost centuries to come. If a team solves a problem during the \(t_a\)th time unit, and the balloon is sent to them during the \(t_b\)th time unit, then the unhappiness of the team will increase by \(t_bt_a\). In order to give out balloons timely, the organizers of the contest have bought a balloon robot. At the beginning of the contest (that is to say, at the beginning of the 1st time unit), the robot will be put on the \(k\)th seat and begin to move around the table. If the robot moves past a team which has won themselves some balloons after the robot's last visit, it will give all the balloons they deserve to the team. During each unit of time, the following events will happen in order:
BaoBao is interested in minimizing the total unhappiness of all the teams. Your task is to select the starting position \(k\) of the robot and calculate the minimum total unhappiness of all the teams according to BaoBao's predictions. InputThere are multiple test cases. The first line of the input contains an integer \(T\), indicating the number of test cases. For each test case: The first line contains three integers \(n\), \(m\) and \(p\) (\(1 \le n \le 10^5\), \(n \le m \le 10^9\), \(1 \le p \le 10^5\)), indicating the number of participating teams, the number of seats and the number of predictions. The second line contains \(n\) integers \(s_1, s_2, \dots, s_n\) (\(1 \le s_i \le m\), and \(s_i \ne s_j\) for all \(i \ne j\)), indicating the seat number of each team. The following \(p\) lines each contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i \le n\), \(1 \le b_i \le 10^9\)), indicating that the \(a_i\)th team solves a problem at time \(b_i\) according to BaoBao's predictions. It is guaranteed that neither the sum of \(n\) nor the sum of \(p\) over all test cases will exceed \(5 \times 10^5\). OutputFor each test case output one integer, indicating the minimum total unhappiness of all the teams according to BaoBao's predictions. Sample Input4 2 3 3 1 2 1 1 2 1 1 4 2 3 5 1 2 1 1 2 1 1 2 1 3 1 4 3 7 5 3 5 7 1 5 2 1 3 3 1 5 2 5 2 100 2 1 51 1 500 2 1000 Sample Output1 4 5 50 HintFor the first sample test case, if we choose the starting position to be the 1st seat, the total unhappiness will be (31) + (11) + (64) = 4. If we choose the 2nd seat, the total unhappiness will be (21) + (31) + (54) = 4. If we choose the 3rd seat, the total unhappiness will be (11) + (21) + (44) = 1. So the answer is 1. For the second sample test case, if we choose the starting position to be the 1st seat, the total unhappiness will be (31) + (11) + (32) + (33) + (64) = 5. If we choose the 2nd seat, the total unhappiness will be (21) + (31) + (22) + (53) + (54) = 6. If we choose the 3rd seat, the total unhappiness will be (11) + (21) + (42) + (43) + (44) = 4. So the answer is 4. 