// Template Begins
#include<bits/stdc++.h>
using namespace std;

#define fio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define endl '\n'
#define vi vector<int>
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll ,ll> pll;
#define max3(a,b,c) max(max((a),(b)),(c))
#define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
#define min3(a,b,c) min(min((a),(b)),(c))
#define min4(a,b,c,d) min(min((a),(b)),min((c),(d)))

// for loops
#define fo(i,begin,end) for(int i = begin;i<end;i++)
const double PI = 3.141592653589793238460;
#define INF 1e18
//#define MOD 1000000007
#define MOD 998244353

#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define set_bits __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define vi vector<int>

typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

#define debug(x) cerr << #x <<" : "; _print(x); cerr << endl;

void _print(ll t) {cerr << t ;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

ll add(ll x,ll y){ll res = x+y;return (res>=MOD? res-MOD:res);}
ll mul(ll x,ll y){ll res = x*y;return (res>=MOD? res%MOD:res);}
ll sub(ll x,ll y){ll res = x-y;return (res<0?res+MOD:res);}
ll lcm(ll x,ll y){ll res = (x*y)/__gcd(x,y);return res;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);}
ll power(ll x,ll y){ll res = 1;x%=MOD ;while(y){if(y&1)res = mul(res,x);y>>=1;x = mul(x,x);}return res;}
ll mod_inv(ll n) {return power(n,MOD-2);}
template<class T> void swap(T *a,T *b){int *temp;temp = a; a = b; b = temp;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << ", ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "{ "; for (T i : v) {_print(i); cerr << ", ";} cerr << "}";}
template <class T> void _print(multiset <T> v) {cerr << "{ "; for (T i : v) {_print(i); cerr << ", ";} cerr << "}";}
template <class T, class V> void _print(map <T, V> v) {cerr << "{ "; for (auto i : v) {_print(i); cerr << ", ";} cerr << "}";}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// a random number in the range l--> r is stored inside p
// auto dist=uniform_int_distribution<int>(l,r);
// int p = dist(rng);

const int N = 5010;
int n;
vector < ll > m;
vector < ll > p;
ll dp[N][N];

int getAns(int ind ,int stalk){
    
    if(ind == n) return 0;
    if(dp[ind][stalk] != -1) return dp[ind][stalk];

    if(stalk >=  m[ind]){
       
        ll y = getAns(ind + 1 , stalk+1);
        return dp[ind][stalk] = y;
    }

    ll x = INF;
    ll y = INF;
    if(stalk + (n - ind-1) >= m[ind]){
        // cout << stalk <<" " <<  ind << " " << m[ind ] << " -- \n";
        x = getAns(ind + 1 , stalk);
    }
    y = p[ind] + getAns(ind + 1 , stalk + 1);
    // cout << x <<" == " << y << endl;
    return dp[ind][stalk] =min(x,y);    
    
}


void solve(){

    cin >> n;
    vector < pair < ll , ll > > store(n);
    m.assign(n,0);
    p.assign(n,0);
    fo(i,0,n){
        fo(j,0,n) dp[i][j] = -1;
    }
    fo(i,0,n ) cin >> store[i].first >> store[i].second;
    
    sort(store.begin() , store.end());
    reverse(store.begin() , store.end());
    fo(i,0,n){
        m[i] = store[i].first;
        p[i] = store[i].second;
    }
    // debug(m)
    // debug(p)
    ll ans = getAns(0 ,  0);
    cout << ans << endl;

    return;

}

int main() {
    fio;
    int T;
    cin >> T;
    while(T--){
        solve();
    }

    cerr << "\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
    return 0;
}

