1251_E2. Voting (Hard Version)
The only difference between easy and hard versions is constraints.

Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you.

There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him p_i coins. The second way is to make m_i other voters vote for you, and the i-th voter will vote for free.

Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m_1 = 1, m_2 = 2, m_3 = 2, m_4 = 4, m_5 = 5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: {5} → {1, 5} → {1, 2, 3, 5} → {1, 2, 3, 4, 5}.

Calculate the minimum number of coins you have to spend so that everyone votes for you.

Input

The first line contains one integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases.

The first line of each test case contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of voters.

The next n lines contains the description of voters. i-th line contains two integers m_i and p_i (1 ≤ p_i ≤ 10^9, 0 ≤ m_i < n).

It is guaranteed that the sum of all n over all test cases does not exceed 2 ⋅ 10^5.

Output

For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you.

Example

Input


3
3
1 5
2 10
2 8
7
0 1
3 1
1 1
6 1
1 1
4 1
4 1
6
2 6
2 3
2 8
2 7
4 4
5 5


Output


8
0
7

Note

In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: {3} → {1, 3} → {1, 2, 3}.

In the second example you don't need to buy votes. The set of people voting for you will change as follows: {1} → {1, 3, 5} → {1, 2, 3, 5} → {1, 2, 3, 5, 6, 7} → {1, 2, 3, 4, 5, 6, 7}.

In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: {2, 5} → {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5, 6}.
```python
from sys import stdin, stdout
import heapq

class MyHeap(object):
   def __init__(self, initial=None, key=lambda x:x):
       self.key = key
       if initial:
           self._data = [(key(item), item) for item in initial]
           heapq.heapify(self._data)
       else:
           self._data = []

   def push(self, item):
       heapq.heappush(self._data, (self.key(item), item))

   def pop(self):
       return heapq.heappop(self._data)[1]

   def print(self):
       for hd in self._data:
            print(hd)
       print('-------------------------------')

def getminnumerofcoins(n, mpa):

    res = 0

    mpa.sort(key=lambda x: (x[0], -x[1]))
    gap = []

    cur = 0
    for i in range(len(mpa)):
        mp = mpa[i]
        #print(mp[0])
        if mp[0] > cur:
            t = [i, mp[0]-cur]
            gap.append(t)
            #cur = mp[0]

        cur += 1

    #print(gap)

    if len(gap) == 0:
        return 0

    hp = MyHeap(key=lambda x: x[1])
    gp = gap.pop()
    lidx = gp[0]
    remaing = gp[1]
    #print(remaing)
    for i in range(lidx, len(mpa)):
        ci = [i, mpa[i][1]]
        hp.push(ci)

    cur = 0
    offset = 0
    for i in range(len(mpa)):
        mp = mpa[i]
        need = mp[0] - cur

        if need > 0:
            for j in range(need):
                if (remaing == 0 or len(hp._data) == 0) and len(gap) > 0:
                    #print(i)
                    lg = gap.pop()
                    while len(gap) > 0 and lg[1] - offset <= 0:
                        lg = gap.pop()
                    for k in range(lg[0], lidx):
                        ci = [k, mpa[k][1]]
                        hp.push(ci)
                    lidx = lg[0]
                    remaing = lg[1] - offset

                c = hp.pop()
                #print(c)
                if c[0] == i:
                    c = hp.pop()
                    #print(c)
                res += c[1]
                cur += 1
                offset += 1
                remaing -= 1

        cur += 1

    return res


if __name__ == '__main__':
    t = int(stdin.readline())

    for i in range(t):
        n = int(stdin.readline())

        mpa = []
        for j in range(n):
            mp = list(map(int, stdin.readline().split()))
            mpa.append(mp)
        res = getminnumerofcoins(n, mpa)
        stdout.write(str(res) + '\n')

```