------
## 最终结论
```python
def min_coins_to_win_elections(test_cases):
    results = []
    
    for n, voters in test_cases:
        # Sort voters by cost p_i in ascending order
        voters.sort(key=lambda x: x[1])
        
        # Initialize variables
        coins_spent = 0
        current_voters = 0
        
        # While loop to ensure enough votes to convince others
        for i in range(n):
            if current_voters >= voters[i][0]:  # Check if we have enough votes
                current_voters += 1
            else:
                coins_spent += voters[i][1]  # Buy the vote
                current_voters += 1
        
        # Append the result of coins spent to results list
        results.append(coins_spent)
    
    return results

# Read number of test cases and the corresponding voters
t = int(input())
test_cases = []
for _ in range(t):
    n = int(input())
    voters = [tuple(map(int, input().split())) for _ in range(n)]
    test_cases.append((n, voters))

# Call the function with input test cases and print the results for each test case
results = min_coins_to_win_elections(test_cases)
for result in results:
    print(result)
```