#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define mod 1000000007
#define fi first
#define se second
#define pii pair<int,int>
//#define int long long
#define endl '\n'
#define pll pair<long long,long long>
//#define LONGLONG_MAX 10000000000000000
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long power(long long a,long long b){
    long long ans=1;
    while(b>0){
        if(b&1){ans=(ans*a)%mod;}
                a=(a*a)%mod;
                b>>=1;
        }
    return ans;
        
}
const long long LONGLONG_MAX=1e16;
vector<pii>v;
long long dp[5003][5003];
int fun(int i,int j,int n){
    if(i==n){
        if(j==0)return 0;
        else return LONGLONG_MAX;
    }
    if(dp[i][j]!=-1)return dp[i][j];
    j=max(j,v[i].fi-i);
    return dp[i][j]=min(fun(i+1,j,n),v[i].se+fun(i+1,j-1,n));
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        v.clear();
        v.resize(n);
        for(int i=0;i<n;i++){
            cin>>v[i].fi>>v[i].se;
            //v[i].se=-v[i].se;
        }
        sort(v.begin(),v.end());
        for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++)dp[i][j]=-1;
        }
        //memset(dp,-1,sizeof(dp));
        cout<<fun(0,0,n)<<endl;
    }
    return 0;
}