codeforces#P1379C. Choosing flowers

    ID: 31295 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>binary searchbrute forcedata structuresdfs and similardpgreedysortingstwo pointers*2000

Choosing flowers

Description

Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly nn flowers.

Vladimir went to a flower shop, and he was amazed to see that there are mm types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the ii-th type happiness of his wife increases by aia_i and after receiving each consecutive flower of this type her happiness increases by bib_i. That is, if among the chosen flowers there are xi>0x_i > 0 flowers of type ii, his wife gets ai+(xi1)bia_i + (x_i - 1) \cdot b_i additional happiness (and if there are no flowers of type ii, she gets nothing for this particular type).

Please help Vladimir to choose exactly nn flowers to maximize the total happiness of his wife.

The first line contains the only integer tt (1t100001 \leq t \leq 10\,000), the number of test cases. It is followed by tt descriptions of the test cases.

Each test case description starts with two integers nn and mm (1n1091 \le n \le 10^9, 1m1000001 \le m \le 100\,000), the number of flowers Vladimir needs to choose and the number of types of available flowers.

The following mm lines describe the types of flowers: each line contains integers aia_i and bib_i (0ai,bi1090 \le a_i, b_i \le 10^9) for ii-th available type of flowers.

The test cases are separated by a blank line. It is guaranteed that the sum of values mm among all test cases does not exceed 100000100\,000.

For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly nn flowers optimally.

Input

The first line contains the only integer tt (1t100001 \leq t \leq 10\,000), the number of test cases. It is followed by tt descriptions of the test cases.

Each test case description starts with two integers nn and mm (1n1091 \le n \le 10^9, 1m1000001 \le m \le 100\,000), the number of flowers Vladimir needs to choose and the number of types of available flowers.

The following mm lines describe the types of flowers: each line contains integers aia_i and bib_i (0ai,bi1090 \le a_i, b_i \le 10^9) for ii-th available type of flowers.

The test cases are separated by a blank line. It is guaranteed that the sum of values mm among all test cases does not exceed 100000100\,000.

Output

For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly nn flowers optimally.

Samples

输入数据 1

2
4 3
5 0
1 4
2 2

5 3
5 2
4 2
3 1

输出数据 1

14
16

Note

In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals 5+(1+24)=145 + (1 + 2 \cdot 4) = 14.

In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals (5+12)+(4+12)+3=16(5 + 1 \cdot 2) + (4 + 1 \cdot 2) + 3 = 16.