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

Vladimir went to a flower shop, and he was amazed to see that there are $m$ 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 $i$-th type happiness of his wife increases by $a_i$ and after receiving each consecutive flower of this type her happiness increases by $b_i$. That is, if among the chosen flowers there are $x_i > 0$ flowers of type $i$, his wife gets $a_i + (x_i - 1) \cdot b_i$ additional happiness (and if there are no flowers of type $i$, she gets nothing for this particular type).

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


-----Input-----

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

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

The following $m$ lines describe the types of flowers: each line contains integers $a_i$ and $b_i$ ($0 \le a_i, b_i \le 10^9$) for $i$-th available type of flowers.

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


-----Output-----

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


-----Example-----
Input
2
4 3
5 0
1 4
2 2

5 3
5 2
4 2
3 1

Output
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 + 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 + 1 \cdot 2) + (4 + 1 \cdot 2) + 3 = 16$.

Difficulty: interview

Starter code:

Solutions:
import sys
input = sys.stdin.readline

t = int(input())
for test in range(t):
    if test:
        input()
    n, m = list(map(int, input().split()))
    a = [-1] * m
    b = [-1] * m

    for i in range(m):
        a[i], b[i] = list(map(int, input().split()))

    l = [(a[i],0,i) for i in range(m)] + [(b[i],1,i) for i in range(m)]
    l.sort(reverse = True)
    
    count = 0
    tot = 0
    best = 0
    used = [False] * m
    for good, typ, ind in l:
        if typ == 0:
            count += 1
            tot += good
            used[ind] = True
            if count == n:
                best = max(best, tot)
                break
        else:
            curr = tot
            curr += good * (n - count)
            if not used[ind]:
                curr -= good
                curr += a[ind]
            best = max(curr,best)
    print(best)


import sys,heapq

input=sys.stdin.readline

for _ in range(int(input())):
    if _!=0:
        s=input()
    n,m=list(map(int,input().split()))
    flower=[]#b,a
    for i in range(m):
        a,b=list(map(int,input().split()))
        flower.append((b,a))
    flower.sort()
    S=0
    N=0
    ans=-1
    que=[]
    if n==1:
        ans=max(flower[i][1] for i in range(m))
        print(ans)
        continue
    for i in range(m):
        b,a=flower[i]
        while que and b>que[0]:
            A=heapq.heappop(que)
            S-=A
            N-=1
        temp=b*(n-1-N)+a+S
        ans=max(temp,ans)
        if N==n-1:
            if que[0]<a:
                S-=que[0]
                heapq.heappop(que)
                S+=a
                heapq.heappush(que,a)
        else:
            heapq.heappush(que,a)
            S+=a
            N+=1

    print(ans)


import sys
input = sys.stdin.readline
for f in range(int(input())):
    n,m=map(int,input().split())
    a=[0]*m
    b=[0]*m
    acop=[0]*m
    bcop=[0]*m
    for i in range(m):
        a[i],b[i]=map(int,input().split())
        acop[i]=a[i]
        bcop[i]=[b[i],i]
    acop.sort(reverse=True)
    bcop.sort(reverse=True)
    mx=0
    i=0
    su=0
    for j in range(m):
        bi=bcop[j][0]
        ii=bcop[j][1]
        ai=a[ii]
        while i<m and acop[i]>bi:
            su+=acop[i]
            i+=1
        maxi=ai
        if i<n:
            maxi+=su
            if i+1<n:
                maxi+=(n-i-1)*bi
            if ai>bi:
                maxi+=bi
                maxi-=ai
            mx=max(maxi,mx)
    if n<=m:
        sun=0
        for i in range(n):
            sun+=acop[i]
        mx=max(mx,sun)
    print(mx)
    blank=input()

import bisect
t = int(input())
for _ in range(t):
    n, m = list(map(int, input().split()))
    flowers = []
    flowers_a = []
    max_b = 0
    for i in range(m):
        a, b = list(map(int, input().split()))
        flowers.append((a, b))
    
    flowers.sort()
    flowers_b = []
    for i in range(m):
        flowers_a.append(flowers[i][0])
        flowers_b.append(flowers[i][1])
    
    val = 0
    prefix_sum = [0]
    for x in flowers_a:
        prefix_sum.append(prefix_sum[-1] + x)
    for i in range(m):
        idx = bisect.bisect_left(flowers_a, flowers_b[i])
        options = m - idx
        if options >= n:
            temp = prefix_sum[-1] - prefix_sum[-n - 1]
            val = max(val, temp)
            continue
        else:
            temp = prefix_sum[-1] - prefix_sum[-options - 1]
            remaining = n - options
            if idx <= i:
                temp += flowers_b[i] * remaining
            else:
                temp += flowers_a[i] + flowers_b[i] * (remaining - 1)
            
            val = max(val, temp)
                

    print(val)
    
    if _ != t - 1:
        blank_line = input()


from bisect import bisect_left

t = int(input())

for case in range(t):
    n, m = list(map(int, input().split()))
    ab = [list(map(int, input().split())) for _ in range(m)]

    aa = [e for e, _ in ab]
    aa.sort()
    acc = [0] * (m + 1)
    for i in range(m, 0, -1):
        acc[i-1] = acc[i] + aa[i-1]

    if n > m:
        ans = 0
    else:
        ans = sum(aa[-n:])

    for a, b in ab:
        i = bisect_left(aa, b)
        cnt = min(m - i, n)
        sm = acc[m - cnt]
        sm += b * (n - cnt)
        if a < b:
            sm -= b
            sm += a

        ans = max(ans, sm)

    print(ans)

    if case != t - 1:
        input()


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]

testCases = int(input())
answers = []
for test in range(testCases):
	#take input
	n, m = input_split()
	arr = []
	barr = []
	for _ in range(m):
		a, b = input_split()
		arr.append(a)
		barr.append(b)

	temp1 = [(arr[i], 1, i) for i in range(m)]
	temp2 = [(barr[i], 2, i) for i in range(m)]

	mixed = temp1 + temp2
	mixed.sort()
	mixed.reverse()

	pointer = 0
	done = 0
	maxi = 0
	score = 0
	unlocked = [False for i in range(m)]
	while(done < n and pointer < 2*m):
		val, typ, index = mixed[pointer]
		if typ == 1:
			done += 1
			score += val
			unlocked[index] = True
			if done == n:
				maxi = max(score, maxi) 
		else:
			if not unlocked[index]:
				potential = score + (n - done - 1)*val + arr[index]
			else:
				potential = score + (n - done)*val 
				#can break here?
			maxi = max(potential, maxi)

		pointer += 1

	ans = maxi
	answers.append(ans)

	if test != testCases - 1:
		blank = input()

print(*answers, sep = '\n')