← Home
write a go solution for Description:
Toxel likes arrays. Before traveling to the Paldea region, Serval gave him an array a as a gift. This array has n pairwise distinct elements.

In order to get more arrays, Toxel performed m operations with the initial array. In the i-th operation, he modified the p_i-th element of the (i-1)-th array to v_i, resulting in the i-th array (the initial array a is numbered as 0). During modifications, Toxel guaranteed that the elements of each array are still pairwise distinct after each operation.

Finally, Toxel got m+1 arrays and denoted them as A_0=a,A_1,ldots,A_m. For each pair (i,j) (0<=i<j<=m), Toxel defines its value as the number of distinct elements of the concatenation of A_i and A_j. Now Toxel wonders, what is the sum of the values of all pairs? Please help him to calculate the answer.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases t (1<=t<=10^4). The description of the test cases follows.

The first line of each test case contains two integers n and m (1<=n,m<=2*10^5) — the length of the array and the number of operations.

The second line of each test case contains n integers a_1,a_2,...,a_n (1<=a_i<=n+m). It is guaranteed that all a_i are pairwise distinct.

Each of the next m lines of each test case contains two integers p_i and v_i (1<=p_i<=n, 1<=v_i<=n+m) — the position of the modified element and its new value. It is guaranteed that the elements of each array are still pairwise distinct after each modification.

It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 2*10^5.

Output Format:
For each test case, print a single integer — the sum of the values of all pairs of arrays.

Note:
In the first test case, the arrays change as follows: [1,2,3]to[underline4,2,3]to[4,underline5,3].

The concatenation of the 0-th array and the 1-st array is requirecancel[1,2,3,4,cancel2,cancel3]. There are 4 distinct elements.

The concatenation of the 0-th array and the 2-nd array is requirecancel[1,2,3,4,5,cancel3]. There are 5 distinct elements.

The concatenation of the 1-st array and the 2-nd array is requirecancel[4,2,3,cancel4,5,cancel3]. There are 4 distinct elements.

Strikethrough elements are duplicates in the array.

Therefore, the answer is 4+5+4=13.

In the second test case, note that the array may remain unchanged after operations.. Output only the code with no comments, explanation, or additional text.