------
## 最终结论
```python
def min_cost_to_win(t, test_cases):
    results = []
    
    for case in test_cases:
        n = case[0]
        voters = case[1]
        
        # Sort voters based on the number of required votes to convince them (m_i)
        voters.sort(key=lambda x: (x[0], x[1]))
        
        # To track the minimum cost to ensure all voters vote for us
        dp = [float('inf')] * (n + 1)
        dp[0] = 0  # No cost to convince 0 voters
        
        for m_i, p_i in voters:
            # We need to check from the back to avoid overwriting the current state
            for j in range(n, 0, -1):
                if j - 1 >= m_i:
                    dp[j] = min(dp[j], dp[j - m_i] + p_i)
        
        # The answer for this case is the minimum cost to convince all n voters
        results.append(min(dp[n], sum(p for _, p in voters)))
    
    return results


# Read input
t = int(input())
test_cases = []

for _ in range(t):
    n = int(input())
    voters = []
    for _ in range(n):
        m_i, p_i = map(int, input().split())
        voters.append((m_i, p_i))
    test_cases.append((n, voters))

# Calculate minimum costs and print results
results = min_cost_to_win(t, test_cases)
for result in results:
    print(result)
```