CF2036B.Startup

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Startup

Arseniy came up with another business plan — to sell soda from a vending machine! For this, he purchased a machine with nn shelves, as well as kk bottles, where the ii-th bottle is characterized by the brand index bib_i and the cost cic_i.

You can place any number of bottles on each shelf, but all bottles on the same shelf must be of the same brand.

Arseniy knows that all the bottles he puts on the shelves of the machine will be sold. Therefore, he asked you to calculate the maximum amount he can earn.

Input

The first line contains one integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1n,k21051 \le n, k \le 2 \cdot 10^5), where nn is the number of shelves in the machine, and kk is the number of bottles available to Arseniy.

The next kk lines contain two integers bib_i and cic_i (1bik,1ci10001 \le b_i \le k, 1 \le c_i \le 1000) — the brand and cost of the ii-th bottle.

It is also guaranteed that the sum of nn across all test cases does not exceed 21052 \cdot 10^5 and that the sum of kk across all test cases also does not exceed 21052 \cdot 10^5.

Output

For each test case, output one integer — the maximum amount that Arseniy can earn.

Note

In the first test case, Arseniy has 33 shelves in the vending machine. He can place, for example, two bottles of the brand 22 on the first shelf and a bottle of the brand 11 on the second shelf. Then the total cost of the bottles would be 6+7+15=286 + 7 + 15 = 28.

In the second test case, he has only one shelf. It is not difficult to show that the optimal option is to place a bottle of the brand 11 on it. Then the total cost will be 1515.

In the third test case, he has as many as 66 shelves, so he can place all available bottles with a total cost of 7+5=127 + 5 = 12.

Samples

4
3 3
2 6
2 7
1 15
1 3
2 6
2 7
1 15
6 2
1 7
2 5
190000 1
1 1000
28
15
12
1000

在线编程 IDE

建议全屏模式获得最佳体验