import sys
from array import array  # noqa: F401
import typing as Tp  # noqa: F401


def input():
    return sys.stdin.buffer.readline().decode('utf-8')


def main():
    from collections import defaultdict
    from heapq import heappop, heappush
    t = int(input())
    ans = [''] * t

    for ti in range(t):
        n = int(input())
        dd: Tp.Dict[int, Tp.List[int]] = defaultdict(list)
        costs = [0] * n

        for i in range(n):
            mi, pi = map(int, input().split())
            dd[mi].append(i)
            costs[i] = pi

        hq = []

        for cnt in range(n):
            for x in dd[cnt]:
                heappush(hq, -costs[x])

            if hq:
                heappop(hq)

        ans[ti] = str(-sum(hq))

    sys.stdout.buffer.write(('\n'.join(ans) + '\n').encode('utf-8'))


if __name__ == '__main__':
    main()
