//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>
//#include <bits/extc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
 
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define int ll
 
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned ll;
 
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second
#define PI 3.14159265
 
//const int MOD = 1e9 + 7;
const int MOD = 998244353;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0}; 
const char dir[] = {'R', 'L', 'D', 'U'};
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
int add(int a, int b){ //(a + b) % 1e9 + 7
    a += b;
    if(a < 0){
        a += MOD;
    }
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}
 
int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}
 
int mult(int a, int b){
    return ((ll) a * b) % MOD;
}
 
void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

signed main(){
    setIO();
    int t; cin >> t;
    while(t--) {
        int n; cin >> n;
        vector<array<int, 2>> p(n);
        for(int i = 0; i < n; ++i) {
            cin >> p[i][0] >> p[i][1];
        } 
        sort(all(p));
        int dp[n][n + 1];
        for(int i = 0; i < n; ++i) {
            fill(dp[i], dp[i] + n + 1, 1e9);
        }
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j <= n; ++j) {
                if(i == 0) {
                    if(i + j >= p[i][0]) {
                        dp[i][j] = 0;
                    }
                    dp[i][j] = min(dp[i][j], p[i][1]);
                    continue;
                }
                if(i + j >= p[i][0]) {
                    dp[i][j] = min(dp[i][j], dp[i - 1][j]);
                }
                if(j < n) dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + p[i][1]);
            }
        }
        cout << dp[n - 1][0] << "\n";
    }
}

//CHECK FOR LONG LONG!!!    
//LONG LONG OVERFLOW??
//TLE: didnt take in all the input?
//bitwise operations come super late
//multiset instead of set?